PDA

View Full Version : What is it with rand()?


Jones
2006.05.29, 08:31 PM
How come rand() always returns the same value? Even after the program has exited, and is restarted. And how can I put a limit on the number when it refuses to let you redefine RAND_MAX?

OneSadCookie
2006.05.29, 08:54 PM
rand is pseudo-random -- that means that for any given seed, it returns the same sequence of numbers. You need to set the seed yourself to something that changes each time your program is run if you want a different sequence each time:

srand(time(NULL));

The standard pattern for limiting the range returned by rand is to use %:

int n = rand() % 100; // n is 0 to 99, but not 100

Also note that rand() is not very random. You might want to do this:

static inline int better_rand(void) { return rand() >> 8; }

Note that that changes RAND_MAX accordingly.

unknown
2006.05.29, 09:02 PM
If you want actual random numbers get one of these: http://www.araneus.fi/products-alea-eng.html :D

Jones
2006.05.29, 09:06 PM
The standard pattern for limiting the range returned by rand is to use %:

int n = rand() % 100; // n is 0 to 99, but not 100

Also note that rand() is not very random. You might want to do this:

static inline int better_rand(void) { return rand() >> 8; }

Note that that changes RAND_MAX accordingly.

I've seen the first two techniques before (time and %, I wasn't sure what the second did specifically though), how exactly does the third work? I believe that ">>" is a c++ thing.

lightbringer
2006.05.29, 09:22 PM
This page describes (http://www.qbrundage.com/michaelb/pubs/essays/random_number_generation.html) what I recommend and use for random number generation (although, I'm certainly no expert on the subject).

Jones
2006.05.29, 09:32 PM
When using time, the value I get back is a very large number. A c file I found on google described the result as being seconds, but that would mean that right now it is roughly 5000 oclock. :wacko:

What am I getting back?

Skorche
2006.05.29, 09:38 PM
When using time, the value I get back is a very large number. A c file I found on google described the result as being seconds, but that would mean that right now it is roughly 5000 oclock. :wacko:

What am I getting back?

From the man page.
The time() function returns the value of time in seconds since 0 hours, 0
minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without
including leap seconds. If an error occurs, time() returns the value
(time_t)-1.

OneSadCookie
2006.05.29, 10:39 PM
"x >> y" means "x shifted right by y bits". C++'s overloaded meaning (for input) is an abomination.

dave05
2006.06.06, 01:48 PM
"x >> y" means "x shifted right by y bits". C++'s overloaded meaning (for input) is an abomination.

agreed///

Strange thing in my project; I create a random world, with randomly generated names for locations, and srand(time(0)) at the beginning of the code, but the world is always generated exactly the same! Names, locations, etc, ...

Could this be because i'm compiling in debug mode? it is sort of useful...

Is there a difference between time(NULL) and time(0)?

psyba
2006.06.06, 02:05 PM
It is not nearly as fast but you could use the system entropy.

http://www.macshadows.com/forums/index.php?showtopic=5912

imikedaman
2006.06.06, 03:45 PM
Strange thing in my project; I create a random world, with randomly generated names for locations, and srand(time(0)) at the beginning of the code, but the world is always generated exactly the same! Names, locations, etc, ...

Could this be because i'm compiling in debug mode? it is sort of useful...

Is there a difference between time(NULL) and time(0)?
You should probably look at documentation for the time() function, since passing a non-null value into that function stores the value of time(NULL) into that pointer location. Make sure you're only calling srand(time(NULL)) once when the program opens, and no more than once.

AnotherJake
2006.06.06, 03:48 PM
If you want actual random numbers get one of these: http://www.araneus.fi/products-alea-eng.html :D
A bit OT: That's pretty darn cool, but they want ~$200 for just one of those! That seems a bit pricey to me, considering they're just using good-ol' standard semiconductor noise to pull the random sample from. But, I guess their market is small... It's such a simple idea, that after all these years, I'm surprised it isn't included standard on modern CPU's.

@lightbringer: That's a great link! The Mersenne Twister from that page just replaced my old bit-shifting RNG :)