rand()

This is a discussion on rand() within the C Programming forums, part of the General Programming Boards category; i wrote this program that outputs 6 random numbers . (for a lottery) problems: 1- the numbers are always the ...

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    rand()

    i wrote this program that outputs 6 random numbers .
    (for a lottery)
    problems:
    1- the numbers are always the same.
    2- how do i contain the values from 1-50 ? the way it is now
    i get a zero.

    code
    ________________________________________________
    #include <stdio.h>
    main()
    {
    int a,b,c,d,e,f;

    a=rand()%50;
    b=rand()%50;
    c=rand()%50;
    d=rand()%50;
    e=rand()%50;
    f=rand()%50;

    printf("%d %d %d %d %d %d\n", a, b, c, d, e, f );

    return 0;
    }
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Use srand(int) to seed the random var generator.
    srand(time(NULL)); works good.
    If you use the same seed, you will get the same numbers. This is usefully for debugging purposes.

    rand() % 50 will return 0 to 49, so add one to the result to get 1 to 50.

  3. #3
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    srand()

    hmm.. i tried that from the Function list i found here, but i didnt have any luck... i'll give it another try..


    thanx..
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    To have your values within a specific range use this:
    rand() % ((max + 1) - min) + min

    Also, you should have probably used the stdlib library..in this case I've also used the time library since I am using time as a seed..I think thats better coding..

    Here's the revised code:

    Good Luck

    A

    [code]
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    int a,b,c,d,e,f;

    srand(time(NULL));

    a=rand()% 50 + 1;
    b=rand()% 50 + 1;
    c=rand()% 50 + 1;
    d=rand()% 50 + 1;
    e=rand()% 50 + 1;
    f=rand()% 50 + 1;

    printf("%d %d %d %d %d %d\n", a, b, c, d, e, f );

    return 0;
    }

    [\code]

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    RAND(3)             Linux Programmer's Manual             RAND(3)
    
    NAME
           rand, srand - random number generator.
    
    SYNOPSIS
           #include <stdlib.h>
    
           int rand(void);
    
           void srand(unsigned int seed);
    
    DESCRIPTION
           The   rand()  function  returns  a  pseudo-random  integer
           between 0 and RAND_MAX.
    
           The srand() function sets its argument as the seed  for  a
           new  sequence  of pseudo-random integers to be returned by
           rand().  These sequences are repeatable by calling srand()
           with the same seed value.
    
           If no seed value is provided, the rand() function is auto-
           matically seeded with a value of 1.
    
    RETURN VALUE
           The  rand()  function  returns  a  value  between  0   and
           RAND_MAX.  The srand() returns no value.
    
    NOTES
           The  versions of rand() and srand() in the Linux C Library
           use the same random number generator as random() and sran-
           dom(),  so the lower-order bits should be as random as the
           higher-order bits.  However, on older  rand()  implementa-
           tions,  the lower-order bits are much less random than the
           higher-order bits.
    
           In Numerical Recipes in C: The Art of Scientific Computing
           (William  H.  Press, Brian P. Flannery, Saul A. Teukolsky,
           William T.  Vetterling;  New  York:  Cambridge  University
           Press,  1990 (1st ed, p. 207)), the following comments are
           made:
                  "If you want to generate a random integer between 1
                  and 10, you should always do it by
    
                         j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
    
                  and never by anything resembling
    
                         j=1+((int) (1000000.0*rand()) % 10);
    
                  (which uses lower-order bits)."
    
           Random-number  generation is a complex topic.  The Numeri-
           cal Recipes in C book (see reference  above)  provides  an
           excellent discussion of practical random-number generation
           issues in Chapter 7 (Random Numbers).
    
           For a more theoretical discussion which also  covers  many
           practical  issues  in  depth, please see Chapter 3 (Random
           Numbers) in Donald E. Knuth's The Art of Computer Program-
           ming,  volume 2 (Seminumerical Algorithms), 2nd ed.; Read-
           ing,  Massachusetts:  Addison-Wesley  Publishing  Company,
           1981.
    
    CONFORMING TO
           SVID 3, BSD 4.3, ISO 9899
    
    SEE ALSO
           random(3), srandom(3), initstate(3), setstate(3)
    
    GNU                        18 May 1995                          1
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 12:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 12:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 09:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21