是否可以在 Java 中生成没有任何预定义边界的随机双精度值?

Is it possible to generate a random double in Java without any pre-defined boundaries?

我阅读了 Math.random() and Random 并且几乎遍历了所有 post 关于在堆栈溢出时生成随机数的内容。

我仍然无法弄清楚如何在没有预先定义的边界的情况下生成随机数。

我的意思是,我知道可以写

Random random = new Random();
int mynumber = random.nextInt(max-min) + min;

但是如果我不想提供这些 maxmin 怎么办? 如果我希望它也被生成怎么办?

归根结底,我的目的是生成一个随机数,在INT_MAXINT_MIN之间也可以是负数。

我尝试了几种方法:

 int max = (int)Math.random();
 int min = (int)Math.random();
 double myNumber = new Random().nextInt(max - min) + min;

不起作用,因为 (int)Math.random() 总是四舍五入为 0。

我也试过:

int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        double myNumber = new Random().nextInt(max - min) + min;

我得到 The call to 'nextInt' always fails as index is out of bounds .

我的目的是生成一个随机的 double,但是 Random ().nextInt(x,y) 只得到 int 作为 x and y。并且 无需 提供 max and min.

的预定义值

你建议我怎么做? 要在程序的每个 运行 处生成一个随机且不同的双精度值?

不带参数直接使用Random#nextInt

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.

int randomInt = ThreadLocalRandom.current().nextInt();
Double.longBitsToDouble(new Random().nextLong())