Duplicate output when using srand for random seeds

I am using srand (time (NULL)); to generate a random seed.

The problem is I am submitting 30+ of the same jobs to the LINUX cluster. If I send them one at a time, everything is fine, but of course I prefer to use a batch job to send all 30 at the same time. Much easier and faster. The problem is that multiple batches of jobs seem to be available at exactly the same time, and I get duplicate results! Can anyone suggest a simple solution?


source to share

2 answers

Consider reading from /dev/random

or /dev/urandom

. They have a higher quality randomness than rand()

(usually just a simple linear congruential generator) and /dev/random

block until enough entropy is created.




Have a look at the new functions in C ++ 11. In particular, std :: random_device. Otherwise a crappy solution is to add pid to the time (NULL).



All Articles