Thread: Fluctuating(decrease-increase) random numbers,how??

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    Fluctuating(decrease-increase) random numbers,how??

    How can you get a random number that fluctuates. See, when I start my program successively,like 25 runs per minute the number just increases is there a way that the random number can fluctuate?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Huh?

    My guess would be that you are seeding the random number generator with "time()", and thus your initial number will be dependent on current time. Since time has a tendency to increase, it will [up to a point] increase. After all, a randum number generator takes a seed, modifies it mathematically (usually a multiply, add and divide/modulo), so changing the seed will affect the actual random number output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    So what should i use instead of time()?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by freddyvorhees View Post
    So what should i use instead of time()?
    Well, that's a good question. You need a number that changes more rapidly/less predictably than time() provides. Unfortunately, it's hard to find one that is portable, available on most systems, and rapidly changing/less predictable.

    Some suggestions:
    In Linux/Unix "gettimeofday" provides microseconds, so that would change 1 million times more often.
    In Windows, GetSystemTime provided millisecond time.
    Both of these return a struct, so you don't just get a large count, you have to combine different parts of the struct to get a "large number".

    Edit: In windows, QueryPerformanceCounter() will give you a 64-bit number that is changing at least on a microsecond basis - often much faster than that.

    Another option would be to use the TSC (TimeStampCounter). There are inline assembler versons here: http://en.wikipedia.org/wiki/RDTSC

    You can also (as "Prelude" on this board suggests) calculate a hash of the time_t value that you get back from time() - this will "shuffle" the changes to the time, which means that it changes more substantially for the same amount of time-change. Have a look at Prelue's site:
    http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Nice, Thanks!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It sounds like the problem is just that srand() is being called multiple times. You can still use time, but you should call srand a total of once in your entire application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM