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?
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?
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.
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.
Nice, Thanks!
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.