-2

i know that when srand() function is called inside a loop it will generate different random numbers, since srand() is inside the loop and the loop is likely done less than a second, therefore we have the same numbers every time. my question is here: why when the srand() is out of the loop it works properly? if the loop is completed less than a second, therefore the seed is again, the same for all numbers...but it changes every time. though the seed is seem to stay the same. try the following code:

int k = 5;

while (k--)
{
    srand(time(0));
    int num1 = rand() % 6;
    cout << num1 << endl;

}
Mostafa Bahri
  • 2,303
  • 2
  • 19
  • 26
  • 1
    might want to show some example code. – AliciaBytes Dec 03 '14 at 20:26
  • 2
    You're not supposed to call srand() in a loop. Call it once at the beginning of main, or better yet if you have a c++11 compiler use `` – Borgleader Dec 03 '14 at 20:27
  • `time()` only has a resolution of one second so if you call it multiple times in quick succession it can quite easily return the same value, meaning you are calling `srand` with the same seed. – Jonathan Potter Dec 03 '14 at 20:28
  • @Borgleader i know how to solve it. i just want to know WHY it works that way! – Mostafa Bahri Dec 03 '14 at 20:29
  • "*i how to solve it*" -- You accidentally a verb. – Keith Thompson Dec 03 '14 at 20:29
  • 1
    `srand` seeds the random-number-generator. If you call it with the same seed as before, you get the same sequence. And the random-number-generator is not magically reset for each loop-iteration if you don't do it manually. So, where's your problem? – Deduplicator Dec 03 '14 at 20:29
  • Take a look at section 13 of the [comp.lang.c FAQ](http://www.c-faq.com/), starting with question 13.15. – Keith Thompson Dec 03 '14 at 20:30
  • @Deduplicator i DO KNOW it wont reset for each loop. but still dont know why it still works even when it is OUT of the loop! i think the same procedure must happen – Mostafa Bahri Dec 03 '14 at 20:31
  • Are you asking why it works when you srand before and after a loop, and not while you are looping? – user2970916 Dec 03 '14 at 20:36
  • @user2970916 yes! that's the question – Mostafa Bahri Dec 03 '14 at 20:37
  • 1
    I think you are lacking a fundamental understanding of how pseudo-random number generators work. Maybe this will help: http://stackoverflow.com/q/3539398/10077 – Fred Larson Dec 03 '14 at 20:38
  • @FredLarson links to an excellent post, much better than my answer below :) – Steve Dec 03 '14 at 20:40

1 Answers1

5

With the same seed, the random number generator will produce the exact same sequence of numbers.

What we want is a seed that is "random", or at least different every time, so that the sequence of random numbers is different every time.

The srand() function uses the system clock as a seed. As mentioned in a comment, if you call srand() multiple times in this one second period and then get a random number, you'll get the same "random" number every time.

Calling srand() outside of a loop is no different, except for the fact that it might be called with at least one second between each call. This means the seed is different, and the numbers generated are different.

Also, as mentioned in another comment, it's usually only necessary to seed the random number generator once at the beginning of your program. You might not want to do this if for some reason you want to generate the same sequence of numbers multiple times.

Steve
  • 6,334
  • 4
  • 39
  • 67
  • well i dont think "it might be called with at least one second between each call". since i run a code with a loop of 36000 calls, that way it must take 36000 seconds at least, but it took no longer than a second... – Mostafa Bahri Dec 03 '14 at 20:42
  • Well, I don't mean in *your program*, I just mean "occasionally calling srand()". Also note that I said "might be called" not "will be called" with at least once second between calls. Like I said, there's not really any reason to call it more than once anyway. – Steve Dec 03 '14 at 20:45
  • So, dont you know another possible reason?! – Mostafa Bahri Dec 03 '14 at 20:51
  • First, calm down. Second, if you're looking for reasons to call srand(), the post linked above by @FredLarson has at least one: when debugging it might be useful to have the same sequence of numbers so outcomes are predictable. Another one might be for unit tests, again for predictability. Of course, if the underlying generator is changed that predictability might change. However, in general, random numbers are supposed to be random - unpredictable. Therefore, in most "normal" cases, you'll only want to call srand once. – Steve Dec 03 '14 at 20:53