+ 2
Why does this script produce the same random numbers for each array
This script allows you to create a struct with 3 arrays. However the same random numbers are generated for each array within the structure. Any suggestions how to improve it? https://code.sololearn.com/c1Zi34URS2BA/#cpp
3 ответов
+ 4
Because you call srand multiple times and not enough time has elapsed for the time to change, it restarts the number at the same point. Call it once at the beginning of your program and you will get what you are looking for.
Passing the same number to srand always returns the same sequence. This is good for debugging new programs as you know what is going to happen each time, which is why it was designed this way.
+ 2
Thanks tutors👍
+ 1
Dont seed rand() in the function, but rather in main(). I once had a very similar case, so this might help you:
https://www.sololearn.com/discuss/893233/?ref=app