Jump to content
  • Advertisement
Sign in to follow this  
mrm83

random not randoming!

This topic is 4207 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Also as a sidenote, debugging is easier when using a deterministic seed, to make reproducing bugs simpler.

Share this post


Link to post
Share on other sites
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...


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!