What is it with rand()?

Jones
Unregistered

Post: #1
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?
Luminary
Posts: 5,139
Joined: 2002.04
Post: #2
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:

Code:
`srand(time(NULL));`

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

Code:
`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:

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

Note that that changes RAND_MAX accordingly.
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
If you want actual random numbers get one of these: http://www.araneus.fi/products-alea-eng.html

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Jones
Unregistered

Post: #4
OneSadCookie Wrote:The standard pattern for limiting the range returned by rand is to use %:

Code:
`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:

Code:
`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.
Member
Posts: 144
Joined: 2004.07
Post: #5
This page describes what I recommend and use for random number generation (although, I'm certainly no expert on the subject).
Jones
Unregistered

Post: #6
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.

What am I getting back?
Sage
Posts: 1,482
Joined: 2002.09
Post: #7
Jones Wrote: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.

What am I getting back?

From the man page.
Quote: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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Luminary
Posts: 5,139
Joined: 2002.04
Post: #8
"x >> y" means "x shifted right by y bits". C++'s overloaded meaning (for input) is an abomination.
dave05
Unregistered

Post: #9
OneSadCookie Wrote:"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)?
Member
Posts: 21
Joined: 2003.01
Post: #10
It is not nearly as fast but you could use the system entropy.

Member
Posts: 161
Joined: 2005.07
Post: #11
dave05 Wrote: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.
Moderator
Posts: 3,567
Joined: 2003.06
Post: #12
unknown Wrote:If you want actual random numbers get one of these: http://www.araneus.fi/products-alea-eng.html
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