Sign in to follow this  
derek7

random number

Recommended Posts

I have a book at home called Numerical Recipes in C which explains it very well and there is lots of implementations of different RNG's.

One thing I remember of hand is that it only has 16 bits of precision. When I go home tonight I'll look up the limitations and post.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Servant of the Lord
Can it do bools or only only ints?


http://www.eskimo.com/~scs/c-faq.com/lib/randrange.html

(For bool, N = 2)

Quote:

No floats or doubles either?


Cast rand() to float or double and divide by RAND_MAX to get a random number from 0 to 1.
(double)rand()/RAND_MAX

Share this post


Link to post
Share on other sites
Well the book I referred to has a long winded explanation of why the system supplied rand() function is not terribly good, which may not be a bad thing depending on your needs.

The book does however state the if you require a random integer between 1 and 10 using the rand() function then this is how you do it

j=1+int(10.0*rand()/(RAND_MAX+1));

rather than the familiar

j=1+(rand()%10);

The reason for this is that the first uses the high order bits which, presumably are more random.

This piece of code gives you a fast floating point number between 0 and 1

This assumes your floating point is IEEE compatible
This comes from the Numerical Recipes in C++ book


unsigned long idum, itemp;
float rand;

idum = 1664525L*idum + 1013904223L;
itemp = 0x3F800000 | (0x007FFFFF & idum);
rand = (*(float *)&itemp) - 1.0;

Share this post


Link to post
Share on other sites
Quote:
Original post by derek7
Does rand() not work well? why many people do random number algorithm? I heard of Mersenne Twister is good.


rand() has serious flaws for serious applications. rand() is Good Enough for most game applications.

I'm not saying you should not take games seriously, I'm saying the requirements of a random number generator in most games are not as stringent as those in scientific or numeric applications where full-spread spectrum, extended repeat ranges, and low-order randomness are important considerations.

Share this post


Link to post
Share on other sites
rand() has 2 big problems:
a) you can only have 1 random number generator
b) it doesn't provide any convenience functions like random-int-in-range or some gaussian randomness.

It's easy to solve both by using boost::random or something similar.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Adam Hamilton
The reason for this is that the first uses the high order bits which, presumably are more random.


Because many use a linear congruential generator which can, for example, alternate between even and odd numbers. Thus, your first 42 random booleans would be:
true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false true false

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