random number generator

A

asdf

I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.
 
P

Pete Becker

asdf said:
I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.

If you've got a copy of TR1, there are random number generators galore
in it. Dinkumware implements all of the TR. Boost has, among other
pieces, the random number generators (although I haven't checked to see
if they're up to date with the changes made for TR1).

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
N

noone

I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.

Are you sure you want (1) included in your range?

Others have given you pointers to RNG literature. One thing I've noticed
that seems to be an almost universal misuse of random number functions
such as the srand() and rand() functions is that programmers who
use them to generate numbers [0..1) virtually NEVER check to see if the
returned value of rand()==RAND_MAX. While the probability of this
happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
end up outside of your intended range.


For simple non-cryptographic uniform distributions the rand() function is
often adequate but for more stringent requirements the BOOST library
provides several RNGs. Of course as with many open source projects, the
documentation is lacking...at least it was when I last used them.
 
S

Steve Pope

noone said:
One thing I've noticed that seems to be an almost universal
misuse of random number functions such as the srand() and rand()
functions is that programmers who use them to generate numbers
[0..1) virtually NEVER check to see if the returned value of
rand()==RAND_MAX. While the probability of this happening is
1/RAND_MAX, you should check because rand()/RAND_MAX could end
up outside of your intended range.

I usually do something like

int x = rand() & 0x3fffffff;
double y = (double) x / (double) 0x40000000;

y is now uniform in [0,1).

A test for RAND_MAX >= 0x3fffffff would be nice as you say.
On my system RAND_MAX is pow(2,31)-1 which is what you'd expect,
so it works.

Steve
 
P

Pete Becker

noone said:
I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.

Are you sure you want (1) included in your range?

Others have given you pointers to RNG literature. One thing I've noticed
that seems to be an almost universal misuse of random number functions
such as the srand() and rand() functions is that programmers who
use them to generate numbers [0..1) virtually NEVER check to see if the
returned value of rand()==RAND_MAX. While the probability of this
happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
end up outside of your intended range.

Better yet, don't check, but map the range so that this isn't a problem:

rand() / ((double)RAND_MAX + 1)
For simple non-cryptographic uniform distributions the rand() function is
often adequate but for more stringent requirements the BOOST library
provides several RNGs. Of course as with many open source projects, the
documentation is lacking...at least it was when I last used them.

There's documentation for the TR1 random number generators in chapter 13
of my book, "The Standard C++ Library Extensions." The Boost generators
are close to what's in TR1.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
P

Pete Becker

Pete said:
noone said:
I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.

Are you sure you want (1) included in your range?

Others have given you pointers to RNG literature. One thing I've noticed
that seems to be an almost universal misuse of random number functions
such as the srand() and rand() functions is that programmers who
use them to generate numbers [0..1) virtually NEVER check to see if the
returned value of rand()==RAND_MAX. While the probability of this
happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
end up outside of your intended range.

Better yet, don't check, but map the range so that this isn't a problem:

rand() / ((double)RAND_MAX + 1)

Just a clarification (since I confused myself): this gives a uniform
distribution over the half-open range [0.0, 1.0), as the message from
"noone" suggests. It does not answer the original question of how to get
a uniform distribution over the closed range [0.0, 1.0].

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top