Re: random real number between 0 (inclusive) and 1 (inclusive)
Roedy Green wrote:
On Sun, 04 Nov 2007 19:35:39 -0000, "(-Peter-)"
<garfieldpbj@gmail.com> wrote, quoted or indirectly quoted someone who
said :
What I need is to generate a lot of random real numbers between 0 and
1, both inclusive (normally it's possible to generate the number
between 0(inclusive) and 1 (exclusive!!!) )
Multiply the number by 1+ulp where ulp in the smallest increment in a
double in the vicinity of 1.0
Are you sure that will work? I have a suspicion that the
rounding of the products will at best relocate the absent value,
gaining the ability to generate 1.0 at the cost of becoming unable
to generate 0.mumble. The effect might be worse than that,
suppressing many 0.mumble values while "piling on" nearby values
and making them more likely.
A safer approach (I think -- trust, but verify) might be
to generate [0,1) values and "reflect" half of them at random:
double r = rand.nextDouble();
if (rand.nextBoolean())
r = 1.0 - r;
see http://mindprod.com/jgloss/ulp.html
In practice it makes so different since the odds of generating a
double bang on 1 is extremely remote.
Agreed. One chance in 9007199254740992 if I haven't botched
the arithmetic. You'd need to generate a number every nanosecond
for three solid months to accumulate that many; probably six or
twelve months before the lack of an exact 1.0 would be statistically
noticeable.
--
Eric Sosman
esosman@ieee-dot-org.invalid