Thread: rand()

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    rand()

    Hi again,

    I know that if I want to get different random values I should use srand() before using rand(), my questions are:

    1) Why using srand() inside a loop will produce the same results?
    Code:
    for (i=0;i<20;++i)
    {
    	srand(time(NULL));
    	printf("%d\n",rand()%((10)+1));
    }
    2) Why rand() (without srand) always return the same value, what seed does it use??


    Thank you very much

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1) Why using srand() inside a loop will produce the same results?
    Probably because you are seeding with the same seed and then taking the first pseudorandom number associated with the sequence generated by that seed, thus it is always the same (but may not be, it depends on your luck).

    Read Prelude's articles on random numbers and using rand().
    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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why using srand() inside a loop will produce the same results?
    The time function only guarantees second resolution. If your loop runs in less than one second, the seed will always be the same.

    >Why rand() (without srand) always return the same value, what seed does it use??
    The default seed for rand is the same as if you used srand(1).
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Thumbs up

    Guys when I use the following loop

    Code:
    for (i=0;i<20;++i)
    {
    	srand(time(NULL));
    	printf("&#37;d\n",rand()%((10)+1));
    }
    I get results lile :
    5,5,5,5,5,5,5,5,5.....
    or
    8,8,8,8,8,8,8,8,8.....
    or
    n,n,n,n,n,n,n,,n,n
    But they all the same number!


    ?? Why is that?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    When you call srand() with the same value, the first call to rand() will always return the same number. That's basically what you're doing.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by salvadoravi View Post
    ?? Why is that?
    Because you reset the "random" number sequence inside the loop by calling srand again, and again, and again, and again, and.... That loop takes way less than a second to run, so time returns the same number, so you're continually re-seeding the RNG with the same number.

    (Of course, all this was mentioned in the post right above your question, but I thought we'd try again.)

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >?? Why is that?
    Did you even bother to read my answer to your question before asking the same thing again?
    My best code is written with the delete key.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > srand(time(NULL));
    Unless your loop takes substantially more than a second per iteration, time() is effectively returning a constant.

    So yes, you're always resetting the seed to the same value, and thus always getting the same answer from rand().

    Call srand() exactly ONCE at the start of the program. Calling it multiple times doesn't add anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Sorry for the stupied question, it makes alot of sense and I should figure it out from Prelude reply . (too much computer for one day....)

    Thanks again

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, 01: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. Question about rand() function
    By learning C++ in forum C++ Programming
    Replies: 1
    Last Post: 12-25-2003, 02:36 PM
  5. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM