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 ?

    Thanks
     
  2. jcsd
  3. Aug 25, 2007 #2

    mgb_phys

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C, Random numbers within 1 second problem
  1. Random numbers (Replies: 13)

  2. C++ random numbers! (Replies: 3)

Loading...