0

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?

Community
  • 1
  • 1
Doon
  • 3,339
  • 5
  • 25
  • 31
  • 3
    Read the manual: `public static final double MIN_VALUE` A constant holding the smallest _positive_ nonzero value of type double, 2-1074. It is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022 and also equal to Double.longBitsToDouble(0x1L). Anyway, your code will fail when you make the minimum value very negative because... you guessed it... Java arithmetic is done in doubles and the largest double minus the negative of the largest double is more than the largest double. It will overflow. – Gene Jun 16 '14 at 21:49

3 Answers3

1

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.

Marco13
  • 53,703
  • 9
  • 80
  • 159
0

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

John Gardner
  • 24,225
  • 5
  • 58
  • 76
0

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;
Gene
  • 46,253
  • 4
  • 58
  • 96