Same Random Numbers within Loop Iterations
In your loop that executes 15 times, you have invoked dh.setDoors() in each iteration. Inside this function, srand(time(0)) is called to initiate the generation of pseudo-random numbers. However, subsequent calls to rand() within the loop result in constant values of carSetter and decider throughout all 15 iterations.
This behavior occurs because srand(time(0)) sets the seed for the random number generator based on the current time. Since the loop executes quickly, time(0) returns the same value every time, resulting in the same sequence of pseudo-random numbers.
To overcome this issue, it is recommended to only call srand(time(0)) once at the start of the program, typically following a process such as:
srand(time(0));
for (int i = 0; i By performing this initialization only once, the pseudo-random number generator will generate a unique sequence of numbers for each iteration of the loop, yielding the expected variation in the values of carSetter and decider.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3