java 在一个范围内生成一个随机偶数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33870759/
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
Generate a random even number inside a range?
提问by m0a
Here's the format I'm following:
这是我遵循的格式:
int randomNum = rand.nextInt((max - min) + 1) + min;
So here's my code. I'm trying to get a random even number between 1 and 100:
所以这是我的代码。我正在尝试获取 1 到 100 之间的随机偶数:
Random rand = new Random();
int randomNum = rand.nextInt((100 - 2) + 1) + 2;
I saw one solution is to multiply the number by 2, but that breaches the intended range.
我看到一种解决方案是将数字乘以 2,但这超出了预期范围。
I thought of doing a bunch of if statements to solve this, but it seems overly complex. Is there a relatively simple solution?
我想过做一堆 if 语句来解决这个问题,但它似乎过于复杂。有没有比较简单的解决方法?
回答by Tim B
Just generate a number from 0 to 49 and then multiply it by 2.
只需生成一个从 0 到 49 的数字,然后将其乘以 2。
Random rand = new Random();
int randomNum = rand.nextInt(100/2) *2;
To do it in a range just add the range in:
要在范围内执行此操作,只需将范围添加到:
int randomNum = startOfRange+rand.nextInt((endOfRange-startOfRange)/2) *2;
Note that startOfRange should be an even number or converted into an even number.
注意 startOfRange 应该是偶数或转换成偶数。
回答by rouble
In Java 1.7 or later, I would use ThreadLocalRandom:
在 Java 1.7 或更高版本中,我会使用ThreadLocalRandom:
import java.util.concurrent.ThreadLocalRandom;
// Get even random number within range [min, max]
// Start with an even minimum and add random even number from the remaining range
public static int randEvenInt(int min, int max) {
if (min % 2 != 0) ++min;
return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1);
}
// Get odd random number within range [min, max]
// Start with an odd minimum and add random even number from the remaining range
public static int randOddInt(int min, int max) {
if (min % 2 == 0) ++min;
return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1);
}
The reason to use ThreadLocalRandom is explained here. Also note, that the reason we +1 to the input to ThreadLocalRandom.nextInt() is to make sure the max is included in the range.
这里解释了使用 ThreadLocalRandom 的原因。另请注意,我们对 ThreadLocalRandom.nextInt() 的输入进行 +1 的原因是确保最大值包含在范围内。
回答by SomeJavaGuy
Here′s a tiny example on how to do it
这是一个关于如何做的小例子
static Random rand = new Random();
public static void main(String[] args) {
for(int i = 0;i<100;++i)
System.out.println(generateEvenNumber(0, 100));
}
private static int generateEvenNumber(int min, int max) {
min = min % 2 == 1 ? min + 1 : min; // If min is odd, add one to make sure the integer division can′t create a number smaller min;
max = max % 2 == 1 ? max - 1 : max; // If max is odd, subtract one to make sure the integer division can′t create a number greater max;
int randomNum = ((rand.nextInt((max-min))+min)+1)/2; // Divide both by 2 to ensure the range
return randomNum *2; // multiply by 2 to make the number even
}
回答by Averroes
Another approach (maybe not the optimal one):
另一种方法(可能不是最佳方法):
- Create an array (or a List) with all the even numbers between 1 and 100. (This is easy with a loop)
- When you need a random even number just take a random number from that array or list (using random with length or size).
- 创建一个数组(或列表),其中包含 1 到 100 之间的所有偶数。(使用循环很容易)
- 当您需要一个随机偶数时,只需从该数组或列表中获取一个随机数(使用带有长度或大小的随机数)。
回答by Abdul Majeed
Best way to generate even random number between 2 and 1000
生成 2 到 1000 之间偶数随机数的最佳方法
Random random = new Random();
int Low = 2;
int High = 1000;
int randomNumber = random.nextInt(High-Low) + Low;
if(randomNumber % 2 != 0){
randomNumber = randomNumber + 1;
}
回答by Runixscape RSPS
private int getRandomNoOdd(int maxValue) {//min value is 0, including zero, max value can potentially be reached by randomness
int random = 0;
while (true) {
random = (int) (Math.random() * (maxValue + 1));
if (random % 2 != 0)
continue;
break;
}
return random;
}
回答by R. drt
You can also get a random number from a range then, if % 2 != 0, add 1.
您还可以从一个范围内获取一个随机数,如果 % 2 != 0,则加 1。
回答by Konstantin Yovkov
After you get the random number between 1
and 100
and multiply it by 2
, you can get the number % 100
. Something like:
当您得到的随机数1
和100
繁衍它的2
,你可以得到的number % 100
。就像是:
int random = rand.nextInt(100);
random = (random * 2) % 100;
This way number
will always be less than 100
and even.
这种方式number
将始终小于100
和偶数。