生成一个范围内的随机偶数?
Generate a random even number inside a range?
这是我遵循的格式:
int randomNum = rand.nextInt((max - min) + 1) + min;
这是我的代码。我正在尝试获取 1 到 100 之间的随机偶数:
Random rand = new Random();
int randomNum = rand.nextInt((100 - 2) + 1) + 2;
我看到一个解决方案是将数字乘以 2,但这超出了预期范围。
我想过做一堆 if 语句来解决这个问题,但它似乎过于复杂。有没有比较简单的解决方法?
得到1
和100
之间的随机数,乘以2
,就可以得到number % 100
。类似于:
int random = rand.nextInt(100);
random = (random * 2) % 100;
这样number
永远小于100
甚至
只需生成一个从 0 到 49 的数字,然后将其乘以 2。
Random rand = new Random();
int randomNum = rand.nextInt(100/2) *2;
要在一个范围内完成,只需将范围添加到:
int randomNum = startOfRange+rand.nextInt((endOfRange-startOfRange)/2) *2;
注意startOfRange应该是偶数或者转换成偶数。
这是一个关于如何操作的小例子
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
}
另一种方法(可能不是最佳方法):
- 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).
生成 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;
}
在 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);
}
解释了使用 ThreadLocalRandom 的原因here。另请注意,我们对 ThreadLocalRandom.nextInt() 的输入 +1 的原因是确保最大值包含在范围内。
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;
}
你也可以得到一个范围内的随机数,如果 % 2 != 0,加 1。
这是我遵循的格式:
int randomNum = rand.nextInt((max - min) + 1) + min;
这是我的代码。我正在尝试获取 1 到 100 之间的随机偶数:
Random rand = new Random();
int randomNum = rand.nextInt((100 - 2) + 1) + 2;
我看到一个解决方案是将数字乘以 2,但这超出了预期范围。
我想过做一堆 if 语句来解决这个问题,但它似乎过于复杂。有没有比较简单的解决方法?
得到1
和100
之间的随机数,乘以2
,就可以得到number % 100
。类似于:
int random = rand.nextInt(100);
random = (random * 2) % 100;
这样number
永远小于100
甚至
只需生成一个从 0 到 49 的数字,然后将其乘以 2。
Random rand = new Random();
int randomNum = rand.nextInt(100/2) *2;
要在一个范围内完成,只需将范围添加到:
int randomNum = startOfRange+rand.nextInt((endOfRange-startOfRange)/2) *2;
注意startOfRange应该是偶数或者转换成偶数。
这是一个关于如何操作的小例子
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
}
另一种方法(可能不是最佳方法):
- 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).
生成 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;
}
在 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);
}
解释了使用 ThreadLocalRandom 的原因here。另请注意,我们对 ThreadLocalRandom.nextInt() 的输入 +1 的原因是确保最大值包含在范围内。
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;
}
你也可以得到一个范围内的随机数,如果 % 2 != 0,加 1。