Click to See Complete Forum and Search --> : Random Numbers in C++


dunno
01-22-2001, 04:37 PM
What's the best way to get a random number in C++?

BrianDrozd
01-22-2001, 05:14 PM
Well, man rand and man drand48 will give you a list of pseudo-random number generators. It really depends on what you want to use random numbers for, and just how 'random' does it have to be. Personally I recommened using drand48() over rand() simply because I trust the distribution will still be more or less 'fair' after I scale drand48() up, rather than scaling down rand().

Sterling
01-22-2001, 06:57 PM
As a note, the man page for drand48 on my system has the following at the bottom:

NOTES:
These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead.

Any particular reason for this, or just standards committees saying that these aren't on many systems?

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

dunno
01-22-2001, 10:11 PM
So why do you think that drand48() is more "random" than rand()?

BrianDrozd
01-23-2001, 09:42 AM
Originally posted by dunno:
So why do you think that drand48() is more "random" than rand()?
I don't. I think drand48() scales better than rand(). More often than not, when I need a random value, I need a random value between a range of 1 and 100 or 0 and 50 or some other limited range. rand() gives results that are evenly distributed across the entire range of integers, but when you try to scale that back through either division or modulous the distribution is no longer even. On the other hand, scaling up from real values between 0 and 1 (not including 1), the range of drand48(), through multiplication will maintain the the distribution. That is why I prefer drand48() over rand().