You’re reseeding the pseudo-random sequence with the same seed before each call to rand
, since the value returned by time
only changes once a second. That causes you to get the same result each time.
Only call srand
once, at the start of the program.
Better still, look at the C++11 <random>
library, which has more sophisticated generators and access to “true” random numbers for seeding them.
1
solved C++ randomized number always same even with time(0) as seed [duplicate]