如何在一定范围内生成随机数?

时间:2022-11-25 19:33:45

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字段中显示结果?随机数必须在用户选择的范围内。因此,用户将输入范围的最大值和最小值,然后我将输出答案。

8 个解决方案

#1


57  

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)。这将返回[0,n-1]范围内的随机int。

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;

#2


12  

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之一。

#3


3  

" 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]中的随机int。

and you can set it in your textview using the setText() method

您可以使用setText()方法在textview中设置它

#4


1  

Also, from API level 21 this is possible:

此外,从API级别21开始,这是可能的:

int random = ThreadLocalRandom.current().nextInt(min, max);

#5


0  

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:

这将随机设置为用户选择的最小值和最大值之间的随机数。然后你会做:

TextView.setText(random.toString());

#6


0  

private int getRandomNumber(int min,int max) {
    return (new Random()).nextInt((max - min) + 1) + min;
}

#7


0  

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;

#8


0  

Random Number Generator in Android If 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

我希望这个答案可能对你有所帮助。如果您想了解有关本文的更多信息,那么您应该阅读本文。随机数发生器

#1


57  

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)。这将返回[0,n-1]范围内的随机int。

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;

#2


12  

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之一。

#3


3  

" 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]中的随机int。

and you can set it in your textview using the setText() method

您可以使用setText()方法在textview中设置它

#4


1  

Also, from API level 21 this is possible:

此外,从API级别21开始,这是可能的:

int random = ThreadLocalRandom.current().nextInt(min, max);

#5


0  

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:

这将随机设置为用户选择的最小值和最大值之间的随机数。然后你会做:

TextView.setText(random.toString());

#6


0  

private int getRandomNumber(int min,int max) {
    return (new Random()).nextInt((max - min) + 1) + min;
}

#7


0  

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;

#8


0  

Random Number Generator in Android If 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

我希望这个答案可能对你有所帮助。如果您想了解有关本文的更多信息,那么您应该阅读本文。随机数发生器