Android 如何生成一定范围内的随机数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21049747/
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
How can I generate a random number in a certain range?
提问by user3182651
How can I create an app that generates a random number in Android using Eclipse and then show the result in a TextView
field? The random number has to be in a range selected by the user. So, the user will input the max and min of the range, and then I will output the answer.
如何使用 Eclipse 在 Android 中创建一个生成随机数的应用程序,然后在TextView
字段中显示结果?随机数必须在用户选择的范围内。因此,用户将输入范围的最大值和最小值,然后我将输出答案。
回答by Phant?maxx
To extend what Rahul Gupta said:
扩展拉胡尔·古普塔 (Rahul Gupta) 所说的话:
You can use the Java function int random = Random.nextInt(n)
.
This returns a random int
in the range [0, n-1]
.
您可以使用 Java 函数int random = Random.nextInt(n)
。
这将返回int
范围内的随机数[0, n-1]
。
I.e., to get the range [20, 80]
use:
即,要获得范围[20, 80]
使用:
final int random = new Random().nextInt(61) + 20; // [0, 60] + 20 => [20, 80]
To generalize more:
概括更多:
final int min = 20;
final int max = 80;
final int random = new Random().nextInt((max - min) + 1) + min;
回答by Narendra Sorathiya
Random r = new Random();
int i1 = r.nextInt(45 - 28) + 28;
This gives a random integer between 28 (inclusive) and 45 (exclusive), one of 28,29,...,43,44.
这给出了一个介于 28(含)和 45(不含)之间的随机整数,即 28,29,...,43,44 之一。
回答by Deplover
Also, from API level 21 this is possible:
此外,从 API 级别 21 开始,这是可能的:
int random = ThreadLocalRandom.current().nextInt(min, max);
回答by Gil SH
private int getRandomNumber(int min,int max) {
return (new Random()).nextInt((max - min) + 1) + min;
}
回答by Rahul Gupta
" the user is the one who select max no and min no ?" What do you mean by this line ?
“用户是选择 max no 和 min no 的人吗?” 你说的这条线是什么意思?
You can use java function int random = Random.nextInt(n)
. This returns a random int in range[0, n-1]).
您可以使用 java 函数int random = Random.nextInt(n)
。这将返回范围 [0, n-1] 中的随机整数)。
and you can set it in your textview using the setText()
method
你可以使用setText()
方法在你的文本视图中设置它
回答by BobbyD17
So you would want the following:
所以你会想要以下内容:
int random;
int max;
int min;
...somewhere in your code put the method to get the min and max from the user when they click submit and then use them in the following line of code:
...在您的代码中的某处放置了在用户单击提交时从用户那里获取最小值和最大值的方法,然后在以下代码行中使用它们:
random = Random.nextInt(max-min+1)+min;
This will set random to a random number between the user selected min and max. Then you will do:
这会将 random 设置为用户选择的 min 和 max 之间的随机数。然后你会这样做:
TextView.setText(random.toString());
回答by Developer
You can use If Random
. For example, this generates a random number between 75 to 100.
您可以使用 If Random
。例如,这会生成 75 到 100 之间的随机数。
final int random = new Random().nextInt(26) + 75;
回答by sumit singh
Random Number Generator in AndroidIf you want to know about random number generator in android then you should read this article till end. Here you can get all information about random number generator in android. Random Number Generator in Android
Android 中的随机数生成器如果您想了解 android 中的随机数生成器,那么您应该阅读本文直至结束。在这里您可以获取有关 android 中随机数生成器的所有信息。Android 中的随机数生成器
You should use this code in your java file.
您应该在 java 文件中使用此代码。
Random r = new Random();
int randomNumber = r.nextInt(100);
tv.setText(String.valueOf(randomNumber));
I hope this answer may helpful for you. If you want to read more about this article then you should read this article. Random Number Generator
我希望这个答案对你有帮助。如果您想阅读有关本文的更多信息,那么您应该阅读本文。随机数发生器
回答by Chitransh Mathur
Below code will help you to generate random numbers between two numbers in the given range:
下面的代码将帮助您在给定范围内的两个数字之间生成随机数:
private void generateRandomNumbers(int min, int max) {
// min & max will be changed as per your requirement. In my case, I've taken min = 2 & max = 32
int randomNumberCount = 10;
int dif = max - min;
if (dif < (randomNumberCount * 3)) {
dif = (randomNumberCount * 3);
}
int margin = (int) Math.ceil((float) dif / randomNumberCount);
List<Integer> randomNumberList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < randomNumberCount; i++) {
int range = (margin * i) + min; // 2, 5, 8
int randomNum = random.nextInt(margin);
if (randomNum == 0) {
randomNum = 1;
}
int number = (randomNum + range);
randomNumberList.add(number);
}
Collections.sort(randomNumberList);
Log.i("generateRandomNumbers", "RandomNumberList: " + randomNumberList);
}