简体   繁体   English

Java中使用Math.random()的随机数

[英]Random numbers with Math.random() in Java

For generating random numbers, I've used the formula: 为了生成随机数,我使用了公式:

(int)(Math.random() * max) + min

The formula I find on Google always seem to be: 我在Google上找到的公式似乎总是如下:

(int)(Math.random() * (max - min) + min)

Which one's right? 哪一个对吗? As far as I know, I've never gotten a number that was out of my range with my formula 据我所知,我的公式中从未得到超出我的范围的数字

Your formula generates numbers between min and min + max. 您的公式会在min和min + max之间生成数字。

The one Google found generates numbers between min and max. 谷歌发现的那个产生了最小和最大之间的数字。

Google wins! 谷歌赢了!

A better approach is: 更好的方法是:

int x = rand.nextInt(max - min + 1) + min;

Your formula generates numbers between min and min + max . 您的公式会在minmin + max之间生成数字。

Random random = new Random(1234567);
int min = 5;
int max = 20;
while (true) {
    int x = (int)(Math.random() * max) + min;
    System.out.println(x);
    if (x < min || x >= max) { break; }
}       

Result: 结果:

10
16
13
21 // Oops!!

See it online here: ideone 在这里在线查看: ideone

Yours: Lowest possible is min, highest possible is max+min-1 你的:最低可能是分钟,最高可能是最高+分钟-1

Google: Lowest possible is min, highest possible is max-1 Google:最低可能是分钟,最高可能是max-1

The first one generates numbers in the wrong range, while the second one is correct. 第一个生成错误范围内的数字,而第二个生成错误。

To show that the first one is incorrect, let's say min is 10 and max is 20. In other words, the result is expected to be greater than or equal to ten, and strictly less than twenty. 为了表明第一个是不正确的,让我们说min是10而max是20.换句话说,结果预计大于或等于10,严格小于20。 If Math.random() returns 0.75 , the result of the first formula is 25 , which is outside the range. 如果Math.random()返回0.75 ,则第一个公式的结果为25 ,超出范围。

if min=10 and max=100 : 如果min=10max=100

(int)(Math.random() * max) + min        

gives a result between 10 and 110, while 给出10到110之间的结果,而

(int)(Math.random() * (max - min) + min)

gives a result between 10 and 100, so they are very different formulas . 给出10到100之间的结果,因此它们是非常不同的公式 What's important here is clarity, so whatever you do, make sure the code makes it clear what is being generated. 这里重要的是清晰度,所以无论你做什么,都要确保代码清楚地说明了生成的内容。

(PS. the first makes more sense if you change the variable 'max' to be called 'range') (PS。如果你将变量'max'更改为'range',第一个更有意义)

Google is right :-) 谷歌是对的:-)

Google's formula creates numbers between: min and max Your formula creates numbers between: min and (min+max) Google的公式会在: min和max之间创建数字您的公式会在: min和(min + max)之间创建数字

int i = (int) (10 +Math.random()*11);

this will give you random number between 10 to 20. 这将给你10到20之间的随机数。

the key here is: 这里的关键是:

a + Math.random()*b

a starting num (10) and ending num is max number (20) - a (10) + 1 (11) 起始编号(10)和结束编号是最大编号(20) - a(10)+ 1(11)

Enjoy! 请享用!

If min = 5 , and max = 10 , and Math.random() returns (almost) 1.0, the generated number will be (almost) 15, which is clearly more than the chosen max . 如果min = 5max = 10 ,并且Math.random()返回(几乎)1.0,则生成的数字将(几乎)为15,这显然大于所选的max

Relatedly, this is why every random number API should let you specify min and max explicitly. 相关地,这就是为什么每个随机数API都应该允许您明确指定min和max。 You shouldn't have to write error-prone maths that are tangential to your problem domain. 您不应该编写与您的问题域相关的容易出错的数学。

There's a small problem with the formula that you found on Google. 您在Google上找到的公式存在一个小问题。 It should be: 它应该是:
(int)(Math.random() * (max - min + 1) + min)
not
(int)(Math.random() * (max - min) + min) . (int)(Math.random() * (max - min) + min)

max - min + 1 is the range in which random numbers can be generated max - min + 1是可以生成随机数的范围

Math.random() 的Math.random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. 返回带有正号的double值,大于或等于0.0且小于1.0。

Now it depends on what you want to accomplish. 现在它取决于你想要完成什么。 When you want to have Numbers from 1 to 100 for example you just have to add 例如,当您想要从1到100的数字时,您只需要添加

(int)(Math.random()*100)

So 100 is the range of values. 所以100是值的范围。 When you want to change the start of the range to 20 to 120 you have to add +20 at the end. 如果要将范围的开头更改为20到120,则必须在结尾添加+20。

So the formula is: 所以公式是:

(int)(Math.random()*range) + min

And you can always calculate the range with max-min, thats why Google gives you that formula. 而且你总是可以用max-min来计算范围,这就是为什么谷歌会给你这个公式。

Math.random() generates a number between 0 (inclusive) and 1 (exclusive). Math.random()生成一个介于0(包括)和1(不包括)之间的数字。

So (int)(Math.random() * max) ranges from 0 to max-1 inclusive. 所以(int)(Math.random() * max)范围从0max-1含)。

Then (int)(Math.random() * max) + min ranges from min to max + min - 1 , which is not what you want. 然后(int)(Math.random() * max) + min范围从minmax + min - 1 ,这不是你想要的。

Google's formula is correct. 谷歌的公式是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM