Sign in to follow this  
MSalley

random integers

Recommended Posts


int n = rand();

?

Is that what you're referring to?

If so, make sure to set a new seed for the generator when you start the program or you'll always get the same random numbers. the seed usually comes in the form of the time.. either from when the operating system started, or real time of day.

Share this post


Link to post
Share on other sites
You can use the rand function.

common usage:

int RandomNumber;
RandomNumber = rand();


You might wish to "randomize" the randomizer. For this you need to 'seed' the randomizer.

common method:

srand((unsigned)time(NULL));

rand/srand - include stdlib.h
time - include time.h

HTH

Chad

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Its the "easiest" way, but its not really correct. The problem is it will not provide an equal chance for all numbers (unless "n" is an even multiple of RAND_MAX+1).

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Its the "easiest" way, but its not really correct. The problem is it will not provide an equal chance for all numbers (unless "n" is an even multiple of RAND_MAX+1).

More important is that many implementations of rand() aren't very good. They favor the high order bits in terms of randomness, but the modulo method uses the low order bits. This is an issue for any value of n.

A better method is
min + (int)(max*rand()/(RAND_MAX+1.0));
Alternatively, you could use a different RNG. boost provides several good ones, including ways of drawing from a range without messing with silly formulas.

CM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Some relavent links:

http://www.gamedev.net/community/forums/topic.asp?topic_id=157952
http://www.gamedev.net/community/forums/topic.asp?topic_id=219156

There are more, but you'll have to *search* for them ...

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
but the modulo method uses the low order bits. This is an issue for any value of n.


?! The modulo method uses all bits except when n is a power of 2 (in which case it becomes equivalent to masking off those high bits).

However,

Quote:
Alternatively, you could use a different RNG. boost provides several good ones, including ways of drawing from a range without messing with silly formulas.


Quoted for emphasis.

Share this post


Link to post
Share on other sites
Quote:
Original post by MSalley
How to you confine it, for example, only give generate an integer between 1 and 10?


This was written for floats but it would be easy enough to adapt for ints.

float random( float Min, float Max )
{
float rnd = rand() / (float) RAND_MAX;
return Min + (rnd * (Max-Min));
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this