Doon
Doon

Reputation: 3749

Generate a Double in the Double domain

According to this question, to create a Double number in a given range, you can use:

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

I'm trying to generate a double number in the double domain [Double.MIN_VALUE, Double.MAX_VALUE] using the same code mentioned above:

package test;

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        double lower = Double.MIN_VALUE;
        double upper = Double.MAX_VALUE;
        Random   rand = new Random();

        for (int i = 0; i < 200; i++) {
            double a = lower + (upper - lower) * rand.nextDouble();
            System.out.println(a);
        }
    }
}

However, I'm getting just positive numbers even after many iterations:

1.436326007111308E308
2.7068601271148073E307
1.266896721067985E308
8.273233207049513E306
1.3338832492644417E308
8.584898485464862E307
1.260909190772451E308
1.5511066198317899E307
1.2083062753983258E308
2.449979496663398E307
7.333729592027637E307
7.832069948910962E307
8.493365260900201E307
5.158907971928131E307
3.126231202546818E307
1.3576316635349233E308
1.0991793636673692E308
6.991662398870649E307

My question is: How to generate a double number in the double range?

Upvotes: 0

Views: 147

Answers (3)

Marco13
Marco13

Reputation: 54631

The results are not what you expected, because MIN_VALUE is the smallest possible positive double value. The smallest possible double value is -Double.MAX_VALUE (note the minus sign).

But you can not simply use lower = -Double.MAX_VALUE, because then, the difference will not be representable as a double, and will overflow.

A first idea would be something like

double d = random.nextDouble() * Double.MAX_VALUE;
if (random.nextBoolean()) d = -d;

to cover the full possible range.


EDIT: A (possibly minor) aside: The proposed method should cover the negative double values as well, and should be correct in the sense that each possible value appears either in its positive or in its negative form with equal probability. However, it will not be able to return the value Double.MAX_VALUE (because the value returned by Random#nextDouble() is strictly smaller than 1.0). But based on the implementation of nextDouble, there anyhow may be double values that will never appear in the output.

Upvotes: 1

Gene
Gene

Reputation: 46960

As I said in the comment and others have also said, MIN_VALUE is positive. But even if you use -Double.MAX_VALUE, your computation will overflow double precision when computing upper - lower because the result will be two times the maximum double! One way around this is:

val = Random.nextDouble();
return (val < 0.5) ? (-2 * val) * Double.MAX_VALUE : (2 * (val - 0.5)) * Double.MAX_VALUE;

Upvotes: 0

John Gardner
John Gardner

Reputation: 25116

Double.MIN_VALUE is the smallest positive value that you can represent as a double.

it is not the largest negative number. that would be -Double.MAX_VALUE

Upvotes: 0

Related Questions