Java:在负双精度范围和正双精度范围内生成随机双精度

时间:2017-04-11 02:19:00

标签: java random

我不太熟悉如何改变Math.random的范围或新的Random()以生成像这样的双精度。我需要能够产生一个介于-10到25美分之间的双倍(我的程序正在处理钱;因此为什么我说美分)。另外一个实例产生90到-75美分之间的随机双倍。当我在寻找答案时,我看到了这一点:

double result = Math.random()*(upper - lower)+ lower;

但是当我将这个想法实现到我的代码中时,当使用正数和负数之间的范围时,范围似乎不起作用...... 我测试了它的上限为0.25,下限为-0.10,但注意到它在我做过的一次测试中打印了0.3567587946356543。因此,我得出结论,我显然没有正确调整范围。

请帮助:(

这是我第一次使用stackoverflow,所以请放轻松我,我会详细说明我说的话没有意义..

这是我使用此代码的现有方法:

public double variation(double price){
    //formula: (Math.random() * range) + min; where range = max - min
    //80% of the time returns amount between -10 & 25 cents
    if(Math.random() > 0.19){ 
        return (Math.random() * 0.35) - 0.10;
    }
    //20% of the time returns amount between -75 & 90 cents
    return (Math.random() * 1.65) - 0.75;
}

我知道这种方法需要双倍的价格才能使用;这是教师要求采取双倍价格而忽视其价值的一部分。所以请忽略它。

5 个答案:

答案 0 :(得分:1)

现在您已将所有代码都包含在问题中,因此我的答案更改为:

打印0.3567587946356543时,当Math.random()中的第一个if(Math.random() > 0.19){来电变为假时,它来自范围-.75到0.90的20%部分

旧答案:

我认为你忘记了较低值的负号

double upper = 0.25;
double lower = -0.10;
double result = Math.random() * (upper - lower) + lower;

答案 1 :(得分:1)

要在-1025之间生成,请尝试: -

Random r = new Random();
int d = r.nextInt(35)+1;
d = d - 10;
double result = d/100.0;

单线

double result = ((r.nextInt(35)+1)-10)/100.0;

答案 2 :(得分:0)

获取长十进制的输出: 它有点粗糙,但它完成了工作。

Random rand = new Random();// creates 'Random' method
int number = rand.nextInt(35)+1;// gets a random integer out of 35
double randDouble = rand.nextDouble();//gets a random double
double finalNumber = ((number + randDouble)-10)/100;//adds the integer and the double,
                                                    //subtracts ten, and divides by 100
System.out.println(finalNumber);// prints the number

以下是输出的一些示例(完全复制和粘贴)。

  • 0.22748959958842008
  • 0.1963085030978741
  • 0.17300671109908058
  • -0.002656673961685705
  • -0.08854411636457332
  • 0.03578255664449403

获得两位输出的输出:

Random rand = new Random(); // creates 'Random' method
    float x = rand.nextInt(35)+1; // gets a random integer(0 through 35)
    float y = x - 10; // subtracts that number by ten(making the new range -10 through 25)
    float z = y/100; // puts number in decimal form

注意此行int number = rand.nextInt(35)+1(长十进制版本)。你最后必须做+1的原因是因为Java将1理解为0.见下面的图表

JAVA   |0 1 2 3 4 5
-------|-----------
HUMANS |1 2 3 4 5 6

答案 3 :(得分:0)

看起来你的公式实际上是正确的:

double result = Math.random() * (upper - lower) + lower;

您可以通过将最小和最大可能随机(即零,1)

进行测试
Random Value | Result
---------------------------
0            | -0.1 (lower)
1            | 0.25
0.5          | 0.075
0.01         | -0.0965

回过头来获取结果的随机值为0.356758795Math.random()必须返回1.305025128,但事实并非如此。 :)

话虽如此,Apache Commons3有这个漂亮的方法RandomUtils.nextDouble(double startInclusive, double endInclusive);为你做这件事。

答案 4 :(得分:0)

我以400.0和-400.0为例。

double min = 400.0; double max = 2 * min; System.out.println(Math.random() * max - min);

输出为

138.0921773815627 212.7567891431654 9.063135840057157 -256.8594518458244 -99.84573995806142 116.53331370219462 33.29613621235126

相关问题