java 在给定范围之间生成奇数随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12594974/
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
Generating an Odd Random Number between a given Range
提问by Sanket
How to generate an odd Random number between a given range..
如何在给定范围之间生成奇数随机数..
For Eg: For range between 1 to 6 .. Random No is 3 or 1 or 5
例如:对于 1 到 6 之间的范围.. 随机数为 3 或 1 或 5
Method for Generating Random No :
产生随机数的方法:
Random_No = Min + (int)(Math.Random()*((Max-Min)+1))
Refer How do I generate random integers within a specific range in Java?
Method For Generating Odd Random No :
生成奇数随机数的方法:
Random_No = Min + (int)(Math.Random()*((Max-Min)+1))
if(Random_No%2 ==0)
{
if((Max%2)==0)&&Random_No==Max)
{
Random_No = Random_No - 1;
}
else{
Random_No = Random_No +1;
}
}
This Function will always convert 2 into 3 and not 1 Can we make this a more random function which can convert 2 sometimes into 3 and sometimes into 1 ??
此函数将始终将 2 转换为 3 而不是 1 我们能否将其变成一个更随机的函数,它可以将 2 有时转换为 3 有时转换为 1 ?
采纳答案by CrazyCasta
Assuming max is inclusive, I'd suggest the following:
假设 max 是包容性的,我建议如下:
if (Max % 2 == 0) --Max;
if (Min % 2 == 0) ++Min;
Random_No = Min + 2*(int)(Math.random()*((Max-Min)/2+1));
It results in even distribution among all the odd numbers.
它导致所有奇数之间的均匀分布。
回答by Bharat Sinha
If you want to include randomness in the direction as well use random number for the same.
如果您想在方向上包含随机性,也可以使用随机数。
int randomDirection = Min + (int)(Math.Random()*((Max-Min)+1));
if(randomDirection%2==0) { // any condition to switch the direction
Random_No = Random_No + 1;
} else {
Random_No = Random_No - 1;
}
回答by Morendo
To do so you need to generate a second pseudo-random number to add or substract 1
为此,您需要生成第二个伪随机数来加或减 1
Random_No = Min + (int)(Math.Random()*((Max-Min)+1))
repartitionNumber =(int)(Math.Random()*((2)) // between 0 and 1
if(Random_No%2 ==0)
{
if(Random_No+1<=Max && Random_No-1>=Min)
{
if(repartitionNumber==0)
Random_No = Random_No + 1;
else
Random_No = Random_No - 1;
}
else if(Random_No+1<=Max)
Random_No = Random_No + 1;
else if (Random_No-1>=Min)
Random_No = Random_No - 1;
}
回答by Jonathon Ashworth
Instead of generating a random number between 0 and 6, generate one between 0 and 5 and round up to the nearest odd number, that way you'll have a perfect distribution (33% for each possibility (1, 3, 5))
不是生成 0 到 6 之间的随机数,而是生成 0 到 5 之间的随机数并向上舍入到最接近的奇数,这样您将拥有完美的分布(每种可能性为 33% (1, 3, 5))
回答by chyx
I wonder why other answers all use the int cast to generate the random number. Why not generate random integerdirectly, which is more accurate than real number way?
我想知道为什么其他答案都使用 int 类型转换来生成随机数。为什么不直接生成随机整数,这比实数方式更准确?
Random rn = new Random();
if(maximum % 2 == 1) maximum = maximum + 1; // turn right bound to even
if(minimum % 2 == 0) minimum = minimum - 1; // turn left bound to odd
int range = (maximum - minimum + 1) / 2;
int randomNum = rn.nextInt(range) * 2 + minimum;
回答by Peter Lawrey
To generate an odd number from a integer you can use n * 2 + 1
Really you are generating random numbers and applying a transformation afterwards
要从整数生成奇数,您可以使用n * 2 + 1
实际上您正在生成随机数并在之后应用转换
int num = min / 2 + random.nextInt((max + 1) / 2 - min / 2);
num = num * 2 + 1;
This will work even if the range is [1,5] [2,5] [2,6] [1,6]
即使范围是 [1,5] [2,5] [2,6] [1,6],这也会起作用
回答by kiriloff
Let the rouding above or below depend on a random epsilon.
让上方或下方的 rouding 取决于随机 epsilon。
Random_No = Min + (int)(Math.Random()*((Max-Min)+1))
if(Random_No%2 ==0)
{
if((Max%2)==0)&&Random_No==Max)
{
Random_No = Random_No - 1;
}
else{
epsilon = Math.Random();
if(epsilon > 0.5)
Random_No = Random_No + 1;
else
Random_No = Random_No - 1;
}
}
回答by rouble
In Java 1.7 or later, I would use ThreadLocalRandom:
在 Java 1.7 或更高版本中,我会使用ThreadLocalRandom:
import java.util.concurrent.ThreadLocalRandom;
// 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);
}
// 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);
}
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 romedius
Mathematically the numbers will not gain anything by rounding up or down in the last step. Instead the first and the last number have a 50% lower chance to get picked over all the other numbers.
从数学上讲,通过在最后一步中向上或向下取整,数字不会得到任何好处。相反,第一个和最后一个数字被选中的几率比其他所有数字低 50%。
Stick with CrazyCasta's or J.A's solution.
坚持使用 CrazyCista 或 JA 的解决方案。
回答by Michael Chen
How about checking the return from Math.random() as a floating number. If its int part is an even number, then convert up/down based on its floating part. Like:
如何检查 Math.random() 的返回值作为浮点数。如果它的 int 部分是偶数,则根据它的浮动部分向上/向下转换。喜欢:
assume Math.random() returned x.y; if x is even, return (y>=0.5)?(x+1):(x-1)
假设 Math.random() 返回 xy;如果 x 是偶数,返回 (y>=0.5)?(x+1):(x-1)
Will this randomized a little?
这会随机一点吗?