mercredi 21 juillet 2021

Getting the same random numbers, possible wrong use of srand() or a coding error?

I bumped into this problem today while i was running my code. My code is supposed to run 10k times and at the end collecting some datas and averages.

Everytime a new run starts, srand generate a radom seed doing

srand(time(NULL))

The code itself is very short and it runs several times during 1 second. I've read that time(NULL) returns the number of seconds since a precise date, so is it possible that for the runs in the same "second" i'm getting the same "random" number? Because i'm getting something like 1000 same numbers and then it changes in another number, repeating itself for other 1000 times approx.

It could be also a coding error but the code looks fine to me.

Is there a way to generate number in a different way if this is the problem? Otherwise i will give a deeper check to my code.

Thank you guys!




Aucun commentaire:

Enregistrer un commentaire