P: n/a

I know how to use rand() to generate random POSITIVEINTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible double value in the range
could come up with equal probability). I'd also like to be able to seed this
generator (e.g., via the clock) so that the same sequence of random values
don't come up every time.
Anybody have an easy and fast (computationally) way to do this? Thanks in
advance...!  
Share this Question
P: n/a

Peteroid wrote: I know how to use rand() to generate random POSITIVEINTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time.
A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles,
first generate a random integer in [0,2^52) and then divide it by 2^52.
Note that all such integers, including 2^52 can be represented exactly as
doubles.
You might want to look at boost::random (see http://www.boost.org/libs/random/index.html for details) for pseudorandom
generators that are good enough to generate 2^52 numbers without cycles.
You need to use a long period generator such as a Mersenne twister to really
get 2^52 values out of it. Your generator will have to generate 64 bit
values of course.
cd  
P: n/a

Thanks, Carl!
You gave me a better way than I was going. I'll create a class that allows
me to generate a floatingpoint number in the range of [0.0.,1.0] (yes,
closed on both sides) of arbitray bitcount resolution up to a limit (51, or
possibly even 63, sounds good, based on your response).
Say the bitcount is N. Generate an Nbit random number by generating N/16
number of random 16bit integers (using rand()), and possibly 1 more for the
N%16 remaining bits (which is masked off the apprograte number of high bits
to get correct number of bytes), and then append them into a double by
shifting and adding. Then divide by a double version of (2^N  1) and that
should (for some values of N) produce the closed interval random
floatingpoint number expressed as a double!
The seed is nor just srand()...
Thanx again...! :)
[==Peteroid==]
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uM**************@TK2MSFTNGP14.phx.gbl... Peteroid wrote: I know how to use rand() to generate random POSITIVEINTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles, first generate a random integer in [0,2^52) and then divide it by 2^52. Note that all such integers, including 2^52 can be represented exactly as doubles.
You might want to look at boost::random (see http://www.boost.org/libs/random/index.html for details) for pseudorandom generators that are good enough to generate 2^52 numbers without cycles. You need to use a long period generator such as a Mersenne twister to
really get 2^52 values out of it. Your generator will have to generate 64 bit values of course.
cd
 
P: n/a

Peteroid wrote: Thanks, Carl!
You gave me a better way than I was going. I'll create a class that allows me to generate a floatingpoint number in the range of [0.0.,1.0] (yes, closed on both sides) of arbitray bitcount resolution up to a limit (51, or possibly even 63, sounds good, based on your response).
Say the bitcount is N. Generate an Nbit random number by generating N/16 number of random 16bit integers (using rand()), and possibly 1 more for the N%16 remaining bits (which is masked off the apprograte number of high bits to get correct number of bytes), and then append them into a double by shifting and adding. Then divide by a double version of (2^N  1) and that should (for some values of N) produce the closed interval random floatingpoint number expressed as a double!
Just be aware that the resulting numbers will very likely not cover all
possible values. The period of a 52 bit number generated from concatenation
of 4 values from a 16 bit rng followed by a mask to 52 bits will be at most
2^18  nowhere near 2^52.
cd  
P: n/a

You may want to read the section on random number generators in
"Numerical Recipes in C". The text is free and online in PDF format by
permission of the publisher, at: http://www.nr.com/
It's very difficult to build a good random number generator. The text
illustrates the problems and some ways around it, but is focused
on float rather than double.
 Tom
"Peteroid" <pe************@msn.com> wrote in message
news:u2**************@TK2MSFTNGP11.phx.gbl... Thanks, Carl!
You gave me a better way than I was going. I'll create a class that allows me to generate a floatingpoint number in the range of [0.0.,1.0] (yes, closed on both sides) of arbitray bitcount resolution up to a limit (51, or possibly even 63, sounds good, based on your response).
Say the bitcount is N. Generate an Nbit random number by generating N/16 number of random 16bit integers (using rand()), and possibly 1 more for the N%16 remaining bits (which is masked off the apprograte number of high bits to get correct number of bytes), and then append them into a double by shifting and adding. Then divide by a double version of (2^N  1) and that should (for some values of N) produce the closed interval random floatingpoint number expressed as a double!
The seed is nor just srand()...
Thanx again...! :)
[==Peteroid==]
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam > wrote in message news:uM**************@TK2MSFTNGP14.phx.gbl... Peteroid wrote: > I know how to use rand() to generate random POSITIVEINTEGER numbers. > > But, I'd like to generate a random DOUBLE number in the range of 0.0 > to 1.0 with resolution of a double (i.e., every possible double value > in the range could come up with equal probability). I'd also like to > be able to seed this generator (e.g., via the clock) so that the same > sequence of random values don't come up every time.
A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles, first generate a random integer in [0,2^52) and then divide it by 2^52. Note that all such integers, including 2^52 can be represented exactly as doubles.
You might want to look at boost::random (see http://www.boost.org/libs/random/index.html for details) for pseudorandom generators that are good enough to generate 2^52 numbers without cycles. You need to use a long period generator such as a Mersenne twister to really get 2^52 values out of it. Your generator will have to generate 64 bit values of course.
cd
 
P: n/a

Thanks for the link Tom!
As it turns out (and unlike what I first asked), I don't actaully need for
every double floatingpoint number to be generated randomly that exists in
[0.0,1.0]. It turns out I'm able to determine the 'resolution' of the
generator that is good enough for my application for what it happens to be
doing at the moment. By 'resolution' I mean the number of possible
equallyspaced floatingpoint random values I need to generate in the
[0.0,1.0] range (e.g., resolution 3 would generate only these values: 0.0,
..5, 1.0).
So I created a class with a 'base' (= resolution1) and let rand() generate
a random number from 0 to base, and then produce this number divided by
base to get it into the [0.0,1.0] range. This is sufficient for my
application.
I realized, based on the responses here, that any random number generator on
a digital computer will never return all possibly real numbers, so I had to
deal with a 'resolution' no matter what. The method I've created deals with
such any (not too big) resolution 'perfectly', and so it works!
Thanks!
[==Peteroid==]
"TOM" <no****@noprovider.nodomain> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... You may want to read the section on random number generators in "Numerical Recipes in C". The text is free and online in PDF format by permission of the publisher, at:
http://www.nr.com/
It's very difficult to build a good random number generator. The text illustrates the problems and some ways around it, but is focused on float rather than double.
 Tom "Peteroid" <pe************@msn.com> wrote in message news:u2**************@TK2MSFTNGP11.phx.gbl... Thanks, Carl!
You gave me a better way than I was going. I'll create a class that
allows me to generate a floatingpoint number in the range of [0.0.,1.0] (yes, closed on both sides) of arbitray bitcount resolution up to a limit
(51, or possibly even 63, sounds good, based on your response).
Say the bitcount is N. Generate an Nbit random number by generating
N/16 number of random 16bit integers (using rand()), and possibly 1 more for the N%16 remaining bits (which is masked off the apprograte number of high bits to get correct number of bytes), and then append them into a double by shifting and adding. Then divide by a double version of (2^N  1) and
that should (for some values of N) produce the closed interval random floatingpoint number expressed as a double!
The seed is nor just srand()...
Thanx again...! :)
[==Peteroid==]
"Carl Daniel [VC++ MVP]"
<cp*****************************@mvps.org.nospam > wrote in message news:uM**************@TK2MSFTNGP14.phx.gbl... Peteroid wrote: > I know how to use rand() to generate random POSITIVEINTEGER numbers. > > But, I'd like to generate a random DOUBLE number in the range of 0.0 > to 1.0 with resolution of a double (i.e., every possible double value > in the range could come up with equal probability). I'd also like to > be able to seed this generator (e.g., via the clock) so that the same > sequence of random values don't come up every time.
A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles, first generate a random integer in [0,2^52) and then divide it by 2^52. Note that all such integers, including 2^52 can be represented exactly
as doubles.
You might want to look at boost::random (see http://www.boost.org/libs/random/index.html for details) for pseudorandom generators that are good enough to generate 2^52 numbers without
cycles. You need to use a long period generator such as a Mersenne twister to really get 2^52 values out of it. Your generator will have to generate 64 bit values of course.
cd
  This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2429
 replies: 5
 date asked: Nov 17 '05
