Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C, Random numbers within 1 second problem

  1. Aug 25, 2007 #1
    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 ?

  2. jcsd
  3. Aug 25, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook