C, Random numbers within 1 second problem

  • Thread starter Thread starter gab_6666
  • Start date Start date
  • Tags Tags
    Numbers Random
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
gab_6666
Messages
8
Reaction score
0
Hi, I try to get different random numbers with srand(time(0)) then rand() but the problem is that the program do it million times within 1 second and return the same value 1 million times.

How could I obtain different values without sleeping the processor ?

Thanks
 
Physics news on Phys.org
srand() resets the starting point (the seed) of the random number generator.
If you use the same value for srand() you will always get the same sequence of random numbers. You should only call srand() once at the start of the program, and even that isn't really necesary.
Calling srand() with time is a quick trick to get a different number everytime the program is started, but obviously if you call it within the same second you get the same valalue.