srand function is returning same values

srand() is a function that sets the seed for the rand() function. What you are doing here is setting the seed to the current time before every rand() you call, which, if called fast enough, will get you the same value (since it will reset to the same seed, which if fast enough will be the same time value).

What you’ll want to do is call srand() once, when the program starts (at the start of your main() function)

Then call rand() every time you want a random number, like you are doing currently, but without calling srand() every time.

Leave a Comment