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

• 08-18-2008
freddyvorhees
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?
• 08-18-2008
matsp
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
• 08-18-2008
freddyvorhees
So what should i use instead of time()?
• 08-18-2008
matsp
Quote:

Originally Posted by freddyvorhees
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
• 08-18-2008
freddyvorhees
Nice, Thanks!
• 08-18-2008
Daved
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.