Why does this script produce the same random numbers for each array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

20th Jan 2018, 9:55 PM
Logomonic Learning
Logomonic Learning - avatar
3 Answers
+ 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.
20th Jan 2018, 10:22 PM
John Wells
John Wells - avatar
+ 2
Thanks tutors👍
20th Jan 2018, 10:36 PM
Logomonic Learning
Logomonic Learning - avatar
+ 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
20th Jan 2018, 10:20 PM
Shadow
Shadow - avatar