Thread: How works the rand function?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    14

    How works the rand function?

    Hi all,
    I'm using the rand function.. I used it correctly but I can't understand why it returns sometimes the same values of a previous computation.
    I use to write:


    When I use this code in a call to function like this:


    Code:
    buildListsOfRandom(){
    
    for  (i=1 TO K){
       createRandomList()
    }
    }
    
    
    createRandomList(){
        srand ( time(NULL) );
        randNum= rand() % 100;
    }
    The values returned for the lists are everytime the same... why??
    Maybe I have to use the srand function in some other place?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because it's not really random, it is a random seeming, but pre-determined, sequence that depends on your hardware. If you do not use a seed, you will get the same sequence on the same machine every time, which is why it is recommended that you use a seed, via srand(), ONCE when your program starts.

    Every time you call srand(), the sequence is reset. Since your loop will happen in a fraction of a second, and time(NULL) returns the current second, you have re-initialized with the exact same seed over and over, meaning the sequence will be the same, and since you have reset it, you will get the first number in that sequence. Over and over.

    Moral: do not call srand() more than once in a single program, unless you are using a completely different seed each time.
    Last edited by MK27; 06-13-2011 at 12:54 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    Quote Originally Posted by MK27 View Post
    Moral: do not call srand() more than once in a single program, unless you are using a completely different seed each time.
    You're right ...I've used the srand() function other times in my code. I was believing that the function srand was to use each time I use it for different reasons...
    So the rule is: Use it once in every execution of a program..?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by angeloulivieri
    So the rule is: Use it once in every execution of a program..?
    To keep it simple, yes. In practice you may have reason to re-seed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    I do it ... but it stil doesn't work!

    I erased completely the srand() function in this procedures (having used it another time in the program)... but the function createRandomList creates the first time some values and the second, third, and so on... the same values

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Make sure you're still only calling srand once. If you call it before making each list, then just as before you're going to get the same list each time.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    Yes.. I'm used it only when I launched th function buildListOfRandom. But in createRandomList i'm creating two different lists each time.. maybe I've to change the seed for the two different lists?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try using the srand(time(NULL)); call once... right at the beginning of your program.

    After that, only use rand() % whatever; to create your lists.

    srand() creates a sequence of random numbers based on a seed value... if you are using time() as your seed, calling it twice in the same second will generate the same list twice. So you really only want to use it once in each run of your program. You want rand() to continue down the same list, not start at the top of a new (possibly duplicated) list.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    uhmmm.. maybe I've to re-control my code because I use the rand function in much points of it. In other cases, in example, I use the srand with a different seed every time because I've different variables changing every time in these functions. I will try tomorrow to do some changes...
    Thanks for your help!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    srand is not tied to anything like variables or anything like that. You can use rand all you want, in all the functions you care to write, for all the variables you need, provided you've used srand once, and exactly once, in the program.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by angeloulivieri View Post
    uhmmm.. maybe I've to re-control my code because I use the rand function in much points of it. In other cases, in example, I use the srand with a different seed every time because I've different variables changing every time in these functions. I will try tomorrow to do some changes...
    Thanks for your help!
    What tabstop said... use srand() once and only once, first line of main in your program. From there use rand() just as often as your heart pleases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() function
    By thunderzone in forum C Programming
    Replies: 10
    Last Post: 05-02-2010, 07:09 AM
  2. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  3. Rand() function...
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 01-26-2006, 09:04 AM
  4. rand() function
    By jduke44 in forum C Programming
    Replies: 9
    Last Post: 10-07-2005, 05:33 PM
  5. Rand() function
    By Da-Nuka in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2004, 08:12 AM

Tags for this Thread