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?
srandonce in your program.0as a pointer value intime(0). In modern C++, especially with C++11 it's a bad idea to use0as a pointer. Usenullptrif the compilers you're targeting support it:srand(time(nullptr)). If the compiler doesn't supportnullptrthenNULLis at least a small improvement over0. Also the<random>library is great, though it may be a bit beyond a first-week student.