如何生成随机数 int
特定范围内的价值?
我尝试了以下,但那些不起作用:
尝试1:
randomNum = minimum + (int)(Math.random() * maximum);
// Bug: `randomNum` can be bigger than `maximum`.
尝试2:
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
// Bug: `randomNum` can be smaller than `minimum`.
在 Java 1.7或更高版本,执行此操作的标准方法如下:
import java.util.concurrent.ThreadLocalRandom;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
看到 相关的JavaDoc。这种方法的优点是不需要显式初始化a java.util.Random中 实例,如果使用不当,可能会造成混淆和错误。
然而,相反地,没有办法明确地设置种子,因此在有用的情况下(例如测试或保存游戏状态或类似情况)可能难以再现结果。在这些情况下,可以使用下面显示的Java之前的1.7技术。
在Java 1.7之前,执行此操作的标准方法如下:
import java.util.Random;
/**
* Returns a pseudo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimum value
* @param max Maximum value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max) {
// NOTE: This will (intentionally) not run as written so that folks
// copy-pasting have to think about how to initialize their
// Random instance. Initialization of the Random instance is outside
// the main scope of the question, but some decent options are to have
// a field that is initialized once and then re-used as needed or to
// use ThreadLocalRandom (if using at least Java 1.7).
//
// In particular, do NOT do 'Random rand = new Random()' here or you
// will get not very good / not very random results.
Random rand;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
看到 相关的JavaDoc。在实践中, java.util.Random中 上课通常比较好 java.lang.Math.random()。
特别是,当标准库中有一个简单的API来完成任务时,不需要重新发明随机整数生成轮。
请注意,这种方法比a更偏向,效率更低 nextInt
方法, https://stackoverflow.com/a/738651/360211
实现这一目标的一个标准模式是:
Min + (int)(Math.random() * ((Max - Min) + 1))
该 Java的 数学库函数Math.random()在范围内生成一个double值 [0,1)
。请注意,此范围不包括1。
为了首先获得特定范围的值,您需要乘以您想要覆盖的值范围的大小。
Math.random() * ( Max - Min )
这将返回范围中的值 [0,Max-Min)
,其中不包括'Max-Min'。
例如,如果你想 [5,10)
,你需要覆盖五个整数值,以便你使用
Math.random() * 5
这将返回该范围内的值 [0,5)
,其中5不包括在内。
现在,您需要将此范围更改为您要定位的范围。您可以通过添加Min值来完成此操作。
Min + (Math.random() * (Max - Min))
您现在将获得该范围内的值 [Min,Max)
。按照我们的例子,这意味着 [5,10)
:
5 + (Math.random() * (10 - 5))
但是,这仍然不包括在内 Max
而你正在获得双倍价值。为了得到 Max
如果包含值,则需要在range参数中添加1 (Max - Min)
然后通过强制转换为int来截断小数部分。这是通过以下方式完成
Min + (int)(Math.random() * ((Max - Min) + 1))
你有它。范围内的随机整数值 [Min,Max]
或者按照例子 [5,10]
:
5 + (int)(Math.random() * ((10 - 5) + 1))
使用:
Random ran = new Random();
int x = ran.nextInt(6) + 5;
整数 x
现在是具有可能结果的随机数 5-10
。
使用:
minimum + rn.nextInt(maxValue - minvalue + 1)
同 Java的8 他们介绍了这种方法 ints(int randomNumberOrigin, int randomNumberBound)
在里面 Random
类。
例如,如果要在[0,10]范围内生成五个随机整数(或单个整数),只需执行以下操作:
Random r = new Random();
int[] fiveRandomNumbers = r.ints(5, 0, 11).toArray();
int randomNumber = r.ints(1, 0, 11).findFirst().getAsInt();
第一个参数表示的大小 IntStream
生成(这是产生无限制的方法的重载方法) IntStream
)。
如果需要进行多次单独调用,可以从流中创建无限原始迭代器:
public final class IntRandomNumberGenerator {
private PrimitiveIterator.OfInt randomIterator;
/**
* Initialize a new random number generator that generates
* random numbers in the range [min, max]
* @param min - the min value (inclusive)
* @param max - the max value (inclusive)
*/
public IntRandomNumberGenerator(int min, int max) {
randomIterator = new Random().ints(min, max + 1).iterator();
}
/**
* Returns a random number in the range (min, max)
* @return a random number in the range (min, max)
*/
public int nextInt() {
return randomIterator.nextInt();
}
}
你也可以这样做 double
和 long
值。
希望能帮助到你! :)
您可以编辑第二个代码示例:
Random rn = new Random();
int range = maximum - minimum + 1;
int randomNum = rn.nextInt(range) + minimum;
只需对您的第一个解决方案进行一些小修改就足够了。
Random rand = new Random();
randomNum = minimum + rand.nextInt((maximum - minimum) + 1);
在此处查看更多信息 Random
ThreadLocalRandom等效于多线程环境的类java.util.Random。在每个线程中本地执行生成随机数。因此,通过减少冲突,我们可以获得更好的表现。
int rand = ThreadLocalRandom.current().nextInt(x,y);
x,y - 间隔,例如(1,10)