java 生成从最小值到最大值的随机整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7219058/
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 Random Integer from Min to Max?
提问by iTurki
I want to generate a random Integer that is:
我想生成一个随机整数,即:
- Inside the range [Min, Max]
inclusive
- The range can be [5,20], [-29, -3] or [-13, 13] (It can be in any range, positive or negative or in between)
- The code is working fine in Android
- 在 [Min, Max] 范围内
inclusive
- 范围可以是 [5,20], [-29, -3] 或 [-13, 13] (它可以在任何范围内,正数或负数或介于两者之间)
- 该代码在 Android 中运行良好
What I got so far is this, but it seems not working with negative ranges !
到目前为止我得到的是这个,但它似乎不适用于负范围!
1 + (int)(Math.random() * ((Max - Min) + 1));
回答by Keith Irwin
I'm pretty sure you want
我很确定你想要
Min+(int)(Math.random()*((Max-Min) + 1));
However, I should point out that the range [-3,-29] has its min and max reversed. (And the same with [5,-13] as was pointed out by Merlyn.)
但是,我应该指出范围 [-3,-29] 的最小值和最大值颠倒了。(与 Merlyn 指出的 [5,-13] 相同。)
If you want to just put in any two numbers for the range, a and b then use the code
如果您只想输入范围内的任意两个数字 a 和 b 然后使用代码
int Min = Math.min(a,b);
int Max = Math.max(a,b);
That way you won't have to worry about the order. This will even work for a==b.
这样你就不必担心订单了。这甚至适用于 a==b。
回答by Mikita Belahlazau
Try this
试试这个
int min = -100;
int max = 100;
Random rand = new Random();
return rand.nextInt(max - min + 1) + min;
回答by Malvolio
Has anyone mentioned that his min and max are reversed? Because that's what is screwing up his code.
有没有人提到他的最小值和最大值颠倒了?因为这就是搞砸他的代码的原因。
EDIT
编辑
The other thing that's messing up his code: it should be Min +
not 1 +
多数民众赞成搞乱了他的代码的另一件事情:它应该是Min +
不1 +
回答by Stephen C
/**
* @param bound1 an inclusive upper or lower bound
* @param bound2 an inclusive lower or upper bound
* @return a uniformly distributed pseudo-random number in the range.
*/
public static int randomInRange(int bound1, int bound2) {
int min = Math.min(bound1, bound2);
int max = Math.max(bound1, bound2);
return min + (int)(Math.random() * (max - min + 1));
}
If the caller can guarantee that bound1
will be less or equal to bound2
than you can skip the step of figuring out the minimum and maximum bounds; e.g.
如果调用者可以保证bound1
小于或等于,则bound2
可以跳过计算最小和最大界限的步骤;例如
/**
* @param min the inclusive lower bound
* @param max the inclusive upper bound
* @return a uniformly distributed pseudo-random number in the range.
*/
public static int randomInRange(int min, int max) {
return min + (int)(Math.random() * (max - min + 1));
}
I haven't tested this on Android, but it should work on any Java or Java-like platform that supports those methods in conformance to the standard (Sun) Java SE specifications.
我没有在 Android 上测试过这个,但它应该可以在任何支持这些方法的 Java 或类 Java 平台上工作,这些方法符合标准 (Sun) Java SE 规范。
回答by Shaftoe2702
You would simply calculate the difference between min and max example -30 and 30 to get: delta <-- absolute value of (30 - (-30)) then find a random number between 0 and delta.
您只需计算最小和最大示例 -30 和 30 之间的差异即可获得:delta <-- (30 - (-30)) 的绝对值,然后在 0 和 delta 之间找到一个随机数。
There is a post related to this already. afterwards translate the number along the number-line by your constant min.
已经有一个与此相关的帖子。然后将数字沿数轴转换为常数最小值。
If using the Random class: 1) is added to the equation here for Random.nextInt(someInt) because nextInt returns: someVal < someInt so you need to be sure to include the boundaries in the functions output.
如果使用 Random 类: 1) 被添加到 Random.nextInt(someInt) 的方程中,因为 nextInt 返回: someVal < someInt 所以你需要确保在函数输出中包含边界。
Something about this code is makes me cautious:
关于这段代码的一些事情让我很谨慎:
return min +
(int)(Math.random() * (max - min + 1));
}
When casting rounding doubles to cast to ints:
当将四舍五入双精度转换为整数时:
int someRandomDoubleRoundedToInteger = (int)(someDouble + 0.5)
Does it work? This is for my benefit, maybe some other newbies will be amused, or maybe I made a blunder so:
它有效吗?这是为了我的利益,也许其他一些新手会被逗乐,或者我犯了一个错误,所以:
lets choose 1 and 10 to begin with. Pick a number between 1 and 10 (Inclusive) I pick 10
让我们先选择 1 和 10。在 1 到 10(含)之间选择一个数字 我选择 10
Math.Random Excludes 0 and 1. so 0.9999999999999999 * (10 - 1 + 1) = 9.999999999999999 cast to int, so lob off everything after the decimal produces 9 the we add 1 to 9 to return 10. (using random anything from (.9 to .999999999999) also will produce 10
Math.Random 不包括 0 和 1。所以 0.999999999999999 * (10 - 1 + 1) = 9.999999999999999 转换为 int,因此在小数点后产生 9 的所有内容都被 lob 删除,我们将 1 加到 9 以返回 10。(使用来自 ( 9 到 .999999999999) 也将产生 10
I want to choose 1 from between 1 and 10.
我想从 1 到 10 中选择 1。
In this case the random method puts out it's closest value to zero say: 0.00000000000000001 * (10 - 1 + 1) cast to int is zero 0 We return 1+0. So that works.
在这种情况下,随机方法输出它最接近零的值,比如:0.00000000000000001 * (10 - 1 + 1) cast to int is zero 0 我们返回 1+0。所以这有效。
Huh, seems to work. Looks like a very, very, very minor bias against 1 since we are never including "0" as a possibility but it should be acceptable.
呵呵,看来可以了。看起来对 1 的偏差非常非常非常小,因为我们从不包括“0”作为可能性,但它应该是可以接受的。
The method works, If the random generator evenly covers the entire area between 0 and 1. How many different numbers can be represented between the {0,1} interval, are they evenly spaced?
该方法有效,如果随机生成器均匀覆盖 0 到 1 之间的整个区域。 {0,1} 区间之间可以表示多少个不同的数字,它们是否均匀分布?
I think it works.
我认为它有效。