6

I have a for loop that runs 15 times, with dh.setDoors() in every iteration.

What setDoors does is call srand(time(0)), then whenever a random number is needed it'll use, for example, carSetter = rand()%3+1. Alternatively, it may use decider = rand()%2+1.

Now, normally decider and carSetter are used in a different ways, but I suspected a problem and made it print out carSetter and decider at every iteration. Here's what came out:

Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
etc...

The values '1' and '2' change when I run it multiple times, but are still the same throughout the 15 times.

Since the loop is running 15 different times, shouldn't carSetter and decider print out a different random number every iteration?

When I don't have srand(time(0)), it works as expected, but there's no seed set, so it's the same sequence of "random" numbers each time, so it's probably a problem with the seed?

6
  • 9
    Seeding should be done once, ever. Read up on how pseudo-random number generators work, and it's probably good to learn some probability theory too while you're at it. Commented Feb 12, 2012 at 17:52
  • I'm only using srand once in setDoors, setDoors is just being repeated 15 times. Is the problem that srand is being repeated 15 times? Commented Feb 12, 2012 at 17:55
  • 3
    @GlassZee: Yes. As Kerrek already said, you should do srand once in your program. Commented Feb 12, 2012 at 17:56
  • 1
    @GregHewgill: ... that is, the actual complete minimal testcase that the OP is debugging with. Commented Feb 12, 2012 at 17:59
  • 1
    A comment on style: You're using 0 as a pointer value in time(0). In modern C++, especially with C++11 it's a bad idea to use 0 as a pointer. Use nullptr if the compilers you're targeting support it: srand(time(nullptr)). If the compiler doesn't support nullptr then NULL is at least a small improvement over 0. Also the <random> library is great, though it may be a bit beyond a first-week student. Commented Feb 12, 2012 at 22:28

4 Answers 4

12

When you call srand(x), then the value of x determines the sequence of pseudo-random numbers returned in following calls to rand(), depending entirely on the value of x.

When you're in a loop and call srand() at the top:

while (...) {
    srand(time(0));
    x = rand();
    y = rand();
}

then the same random number sequence is generated depending on the value that time(0) returns. Since computers are fast and your loop probably runs in less than a second, time(0) returns the same value each time through the loop. So x and y will be the same each iteration.

Instead, you only usually need to call srand() once at the start of your program:

srand(time(0));

while (...) {
    x = rand();
    y = rand();
}

In the above case, x and y will have different values each time through the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Put on my blue psuedo shoes...
3

Every time you invoke srand(time(0)), you're seeding the pseudo-random number generator, imbuing it with a new pseudo-random sequence of numbers. The sequence is different depending on what the argument to srand is, and in this instance you use time(0) so, assuming you call your program at most once per second, you'll always get a new sequence. When you call rand(), you just get the next number in this sequence.

However, since you've decided to call srand multiple times in your program, and because your program is fast (i.e. time(0) isn't changing), all you're doing is repeatedly resetting the PRNG to the same sequence. That's why you're always getting the same values - you keep reseeding the PRNG to be the same sequence, and this also moves the cursor to the beginning of the sequence.

Seed once. Once.

Comments

2

Like Kerrek said, seeding is done only once, in the beginning of the program. After one srand call rand as many times as you like.

Comments

2

You need to run srand in the beginning of your program, for example in the main-function.

When you run srand(time(0)) at the top of the function in which you are using rand(), you are likely to give it the same seed every time. time(0) gives the time in seconds, so you would need to avoid calling setDoors twice within the same second in order to get different numbers every time. If you do call setDoors twice within the same second, the random seed will be the same, and subsequent calls to rand() will generate the same sequence of pseudorandom numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.