random real number between 0 (inclusive) and 1 (inclusive)

P

(-Peter-)

Hi..

I've been searching at the Internet, and in this newsgroup about this
issue, but have not been able to find what I'm searching...

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!!!) )

Can anyone tell me how this is done?

/Peter
 
R

Roedy Green

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

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.
 
P

(-Peter-)

In practice it makes so different since the odds of generating a
double bang on 1 is extremely remote.
--

What if I need 2 billion of random numbers??

Would it statistically happen when?

/Peter
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

(-Peter-) said:
I've been searching at the Internet, and in this newsgroup about this
issue, but have not been able to find what I'm searching...

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!!!) )

private static Random rng = new Random();
....
double r = rng.nextInt(1000000001)/1000000000.0;

or something similar.

Arne
 
P

Patricia Shanahan

Roedy said:
Multiply the number by 1+ulp where ulp in the smallest increment in a
double in the vicinity of 1.0

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.

I think that would introduce a non-uniformity.

[I'm using "^" to mean exponentiation, not xor.]

The problem is that there are 2^53 normalized doubles in the range
[0,1), and 2^53 nextDouble results. If you somehow map them to the
(2^53)+1 normalized doubles in [0,1], one of the doubles has to be
impossible, creating a bias.

How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Patricia
 
P

(-Peter-)

private static Random rng = new Random();
...
double r = rng.nextInt(1000000001)/1000000000.0;

or something similar.

Arne

Hi.. And thanks..

Have you got an idea of how big the numbers should be chosen (what is
the maximum, and would it take longer time to generate?)

/peter
 
C

Christian

(-Peter-) said:
What if I need 2 billion of random numbers??

Would it statistically happen when?

/Peter

nope it would not happen ... not with just a billion of tries!
 
M

Michael Jung

Patricia Shanahan said:
Roedy said:
Multiply the number by 1+ulp where ulp in the smallest increment in a
double in the vicinity of 1.0
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.
I think that would introduce a non-uniformity.
[I'm using "^" to mean exponentiation, not xor.]
The problem is that there are 2^53 normalized doubles in the range
[0,1), and 2^53 nextDouble results. If you somehow map them to the
(2^53)+1 normalized doubles in [0,1], one of the doubles has to be
impossible, creating a bias.
How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Instead of obtaining randomly ("rolling") a long that way, see if you
roll 2^53; anytime you miss bit 1 you can stop. If you actually hit
2^53, you have a 1, otherwise roll in [0,1). I think that is simpler
and you can more easily adapt that to other random number generators
which work with half-open intervals.

On the other hand, the OP only needed 2 billion numbers. That's
approx. 2^31. It'll take a few times generating before noticing that
"1" is missing.

Michael
 
C

Christian

(-Peter-) said:
Okay..

But I do still prefer Arne's method - isn't it the best?

/peter

I would make my decision based on what is most important to me...
if you need 2 billion bumbers.. performance might be a significant issue..
so I would probably choose what ever is fastest...
if performance is not an issue.. I would take what ever looks most
readable in the code..

best is relative...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

(-Peter-) said:
Have you got an idea of how big the numbers should be chosen (what is
the maximum, and would it take longer time to generate?)

With int you can go up to 2 billion.

With long you can go up to a very big number.

It should be very fast.

Note that if you need the low bits of the double to be
uniform distributed you need to use at least 2^53 and
therefore long.

Arne
 
R

Roedy Green

What if I need 2 billion of random numbers??

Would it statistically happen when?

roughly speaking one in 2^64 = 9,223,372,036,854,775,808

It is actually somewhat smaller than that since nextDouble produces
0..1 not the whole range.

perhaps 2^7 = 128 times smaller. In any case quite a bit longer than
you will likely generate numbers.
 
P

Piotr Kobzda

Patricia Shanahan wrote:

[...]
How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Or, instead of using nextInt() calls, the following should work as well:

Random r = new Random() {
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27) + next(1))
/ (double)(1L << 53);
}
};


piotr
 
P

Patricia Shanahan

(-Peter-) said:
What if I need 2 billion of random numbers??

Would it statistically happen when?

There are 2^53 values returned by nextDouble, the normalized doubles in
[0,1). 2e9/2^53 is about 2.22e-7, a probability less than one in 4 million.

Patricia
 
P

Patricia Shanahan

Piotr said:
Patricia Shanahan wrote:

[...]
How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Or, instead of using nextInt() calls, the following should work as well:

Random r = new Random() {
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27) + next(1))
/ (double)(1L << 53);
}
};

If you are going to do that you have to extend Random, because next is
protected.

Patricia
 
E

Eric Sosman

Roedy said:
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.
 
D

Daniel Pitts

Sorry if this is a double post, network problems...

Patricia said:
Piotr said:
Patricia Shanahan wrote:

[...]
How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Or, instead of using nextInt() calls, the following should work as well:

Random r = new Random() {
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27) + next(1))
/ (double)(1L << 53);
}
};

If you are going to do that you have to extend Random, because next is
protected.

Patricia
He did extend Random.
Although, my argument would be that he shouldn't override nextDouble,
but create a new method, as the new method has different semantics than
the original nextDouble.
 
P

Patricia Shanahan

Daniel said:
Sorry if this is a double post, network problems...

Patricia said:
Piotr said:
Patricia Shanahan wrote:

[...]

How about first obtaining a long in the range [0,2^53]. That could be
done by combining the results of a couple of nextInt calls. Then divide
by (double)(1L << 53).

Or, instead of using nextInt() calls, the following should work as well:

Random r = new Random() {
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27) + next(1))
/ (double)(1L << 53);
}
};

If you are going to do that you have to extend Random, because next is
protected.

Patricia
He did extend Random.
Although, my argument would be that he shouldn't override nextDouble,
but create a new method, as the new method has different semantics than
the original nextDouble.

Yup, I realized that, and canceled my message just after I sent it, but
cancel does not always catch up :-(

I agree that the new method should be given a different name, because of
the different semantics.

Patricia
 
E

Eric Sosman

(-Peter-) said:
What if I need 2 billion of random numbers??

Would it statistically happen when?

You should be able to work this out in your head.
Two billion ~= 2**31. The number of distinct doubles
returned by nextDouble() = 2**53. Two billion samples
thus represents 2**(31-53) = 2**(-22) of the possible
range, about one four-millionth.

In other words: If you generate two billion numbers
apiece from two random sources A and B, one with output
in [0,1) and the other in [0,1], there is only one chance
in four million that you'd be able to tell which is which.

To put it yet another way: The population of Costa Rica
is approximately four million, but you happen to know that
one of these "people" is actually a space alien. You have
a special detector that can test whether someone is a space
alien or an authentic homo sapiens, but you can use it only
once: The detector's theta ray generator shorts out and
fuses to a useless mass on its first and only discharge.
Here's your plane ticket to Costa Rica; your mission is
to bring back the ET. What do you think of your chances?
 
P

(-Peter-)

(-Peter-) said:
On 4 Nov., 21:28, Roedy Green <[email protected]>
wrote:
What if I need 2 billion of random numbers??
Would it statistically happen when?

You should be able to work this out in your head.
Two billion ~= 2**31. The number of distinct doubles
returned by nextDouble() = 2**53. Two billion samples
thus represents 2**(31-53) = 2**(-22) of the possible
range, about one four-millionth.

In other words: If you generate two billion numbers
apiece from two random sources A and B, one with output
in [0,1) and the other in [0,1], there is only one chance
in four million that you'd be able to tell which is which.

To put it yet another way: The population of Costa Rica
is approximately four million, but you happen to know that
one of these "people" is actually a space alien. You have
a special detector that can test whether someone is a space
alien or an authentic homo sapiens, but you can use it only
once: The detector's theta ray generator shorts out and
fuses to a useless mass on its first and only discharge.
Here's your plane ticket to Costa Rica; your mission is
to bring back the ET. What do you think of your chances?

thank you for the comments, and the funny example...

/peter
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top