random not randoming!

Started by
15 comments, last by Rhaythe 17 years, 1 month ago
i have this code. int num; num = rand()%5; yes, i get a random number for num. BUT, the random numbers are the same everytime i run the program!! im not getting a different set of random numbers! 1st time i get 1 3 4 2, 2nd program run i get 1 3 4 2, 3rd program run i get 1 3 4 2, i keep getting the same set of numbers! why?
Advertisement
You have to seed srand() first. The most common way is to seed using your computer's clock.

#include <ctime>int main() {   srand(time(NULL));   int num = rand()%5;}
oh !
thanks!
ur my hero~
Just to explain this a little:

Computers are actually really bad at random numbers. Everything about them is built to be deterministic - in general, if you're going to compute something, you want to compute it the same way every time. The C library's rand() function is what's called a "pseudo-random number generator" - it generates a sequence of numbers that seem to be unconnected but are actually the result of following a formula involving assorted large numbers and powers and so on. The result of the formula the first time is used as the input the second time.

What this does mean is that if the input the first time is always the same, then the sequence of random numbers produced will always be the same. This input number is called the 'seed.'

To get around this, you set the seed according to some factor that is likely to be different every time you run the program - time of day, total number of mouse button presses since the system was turned on, whatever you like. time() returns the number of seconds elapsed since the beginning of 1970, and that's always going to be different each time you run your program, so by using time() as the seed you'll always get a different sequence of 'random' numbers.

Note that this does have some convenient side-effects - for example, if you want to implement an 'action replay' feature in your game, you can deliberately set the seed to a particular value to get the same sequence of random numbers each time you run the replay.

It does have its downsides though. Random numbers are often useful in cryptographic applications, and if you can predict what those numbers are going to be then it can make the cryptography easier to crack. That's why you can buy specialist hardware that uses things like radioactive decay to produce much less predictable random numbers.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

It can be really useful though, to have pseudo-random numbers.

On our game client when a hero attacks and we have to calculate damage.
We use a certain seed (which the server also knows - it can be as simple
as incrementing the seed-number each time we get a damage packet),
and using that seed we start to get several random numbers to do all
calculations for the damage event. The actual damage is displayed (for
quick client responsiveness).

The server will do the same calculation, and since it has the same
seed (an thus sequence of random numbers) both client & server will
get to the same damage (unless the client is hacked, which doesn't
matter since the client only calculated it for displaying and is
actually waiting for server confirmation)

Just my 2ct :)
visit my website at www.kalmiya.com
Quote:Original post by mrm83
oh !
thanks!
ur my hero~

Don't forget to call srand(time(NULL)); only once at the beginning of your program. It is not necessary or wise to call srand before each call to rand.
And as a sidenote, if your using threads, each thread needs its own seed.
visit my website at www.kalmiya.com
Also as a sidenote, debugging is easier when using a deterministic seed, to make reproducing bugs simpler.
Would it be possible to generate a random value from a range by reading the timestamp counter (RDTSC) and them mod this result with the range you're interseted in

eg.

RandomNumber = (RDTSC % (Highest - Lowest)) + Lowest

Would this make a good RNG for smallish ranges if this is called right after a user presses a key, moves the mouse, etc...


That would probably not give you a very random distribution at all. Just use rand(). If the RDTSC was a good source of random values, the people who wrote rand() for your platform would have probably used it themselves anyway.

This topic is closed to new replies.

Advertisement