Java - 获取从 100 到 999 的随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32534601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - getting a random number from 100 to 999
提问by Jaaavaaaaa
Please tell me whether I am correct or not with the following line.
请告诉我我是否正确与以下行。
int x = ((int)(Math.random() * 100000)) % 1000;
This line always give me a 3
digit number 100
to 999
?
这条线总是给我一个3
位数字100
来999
?
Is there an easier way to type this out? Did I over complicate this code?
有没有更简单的方法来输入这个?我是否使这段代码过于复杂?
采纳答案by Ben M.
There's a better way to get random numbers, and that's with java.util.Random
. Math.random()
returns a double (floating-point) value, but based on your request of a 3-digit number, I'm going to assume that what you really want is an integer. So here's what you do.
有一个更好的方法来获取随机数,那就是java.util.Random
. Math.random()
返回一个双精度(浮点数)值,但根据您对 3 位数字的请求,我将假设您真正想要的是一个整数。所以这就是你要做的。
// initialize a Random object somewhere; you should only need one
Random random = new Random();
// generate a random integer from 0 to 899, then add 100
int x = random.nextInt(900) + 100;
回答by Paul Boddington
You are not correct. That can produce numbers under 100
. The most familiar way is random.nextInt(900)+100
. Here random
is an instance of Random
.
你不正确。这可以在100
. 最熟悉的方式是random.nextInt(900)+100
。这random
是 的一个实例Random
。
Before Java 7 there was no reason to create more than one instance of Random
in your application for this purpose. Now there's no reason to have any, as the best way is
在 Java 7 之前,没有理由Random
为此目的在您的应用程序中创建多个实例。现在没有理由拥有任何东西,因为最好的方法是
int value = ThreadLocalRandom.current().nextInt(100, 1000);
回答by Sweeper
I don't think what you did here is appropriate. You should create a Random
object instead.
我认为你在这里所做的不合适。您应该Random
改为创建一个对象。
Random random = new Random();
int randomNumber = random.nextInt(900) + 100;
Now randomNumber
must be three digit.
现在randomNumber
必须是三位数。
I wrote a program to test whether your method is correct:
我写了一个程序来测试你的方法是否正确:
int x = 0;
int count = 0;
do {
x = ((int)(Math.random() * 100000)) % 1000;
count++;
} while (Integer.toString(x).length() == 3);
System.out.println ("Finished after " + count + " tries");
And it says "Finished after 4 tries", which means that your method is not correct.
并显示“在 4 次尝试后完成”,这意味着您的方法不正确。
回答by Kiran Indukuri
There is a bug in the code. It can return numbers less than 100 as well. The following link Math.random() explainedhas the solution.
代码中有一个错误。它也可以返回小于 100 的数字。以下链接Math.random() 解释了解决方案。
回答by CoderCroc
Math.random()
returns value between 0.0
(including) and 1.0
(excluding) sayit returns 0.05013371..
(for example) than your method will do the following operation,
Math.random()
之间的回报值0.0
(包括)和1.0
(不含)说,它返回0.05013371..
(例如)比你的方法将做如下操作,
0.05013371.. * 100000 = 5013.371...
(int) 5013.371... = 5013
5013 % 1000 = 13 // Incorrect
But other way aroundyou can still use Math.random()
in a different way to solve this,
但是其他方式你仍然可以用Math.random()
不同的方式来解决这个问题,
int upperBound = 999;
int lowerBound = 100;
int number = lowerBound + (int)(Math.random() * ((upperBound - lowerBound) + 1));
Explanation,
解释,
100 + (int)((Number >= 0.0 and < 1.0) * (999 - 100)) + 1;
100 + (int)((Number >= 0.0 and < 1.0) * (899)) + 1;
MINThis can return,
MIN这可以返回,
100 + (int)(0.0 * (899)) + 1;
100 + 0 + 1
101
MAXThis can return,
MAX这个可以返回,
100 + (int)(0.9999.. * (899)) + 1;
100 + 898 + 1
999
NOTE: You can change upperand lowerbound accordingly to get required results.
注意:您可以相应地更改上限和下限以获得所需的结果。