如何在Java中生成特定范围内的随机整数?
如何在特定范围内生成随机如何在Java中生成特定范围内的随机整数?,java,random,integer,java-8,Java,Random,Integer,Java 8,如何在特定范围内生成随机int值 我尝试了以下方法,但这些方法不起作用: 尝试1: randomNum = minimum + (int)(Math.random() * maximum); Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i; RandomUtils random = new RandomUtils(); ran
int
值
我尝试了以下方法,但这些方法不起作用:
尝试1:
randomNum = minimum + (int)(Math.random() * maximum);
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
RandomUtils random = new RandomUtils();
random.nextInt(0, 0); // returns 0
random.nextInt(10, 10); // returns 10
random.nextInt(-10, 10); // returns numbers from -10 to 10 (-10, -9....9, 10)
random.nextInt(10, -10); // throws assert
import junit.framework.Assert;
import java.util.Random;
public class RandomUtils extends Random {
/**
* @param min generated value. Can't be > then max
* @param max generated value
* @return values in closed range [min, max].
*/
public int nextInt(int min, int max) {
Assert.assertFalse("min can't be > then max; values:[" + min + ", " + max + "]", min > max);
if (min == max) {
return max;
}
return nextInt(max - min + 1) + min;
}
}
错误:randomNum
可以大于最大值
尝试2:
randomNum = minimum + (int)(Math.random() * maximum);
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
RandomUtils random = new RandomUtils();
random.nextInt(0, 0); // returns 0
random.nextInt(10, 10); // returns 10
random.nextInt(-10, 10); // returns numbers from -10 to 10 (-10, -9....9, 10)
random.nextInt(10, -10); // throws assert
import junit.framework.Assert;
import java.util.Random;
public class RandomUtils extends Random {
/**
* @param min generated value. Can't be > then max
* @param max generated value
* @return values in closed range [min, max].
*/
public int nextInt(int min, int max) {
Assert.assertFalse("min can't be > then max; values:[" + min + ", " + max + "]", min > max);
if (min == max) {
return max;
}
return nextInt(max - min + 1) + min;
}
}
错误:randomNum
可以小于最小值
使用:
minimum + rn.nextInt(maxValue - minvalue + 1)
Random ran = new Random();
int x = ran.nextInt(6) + 5;
在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);
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;
}
看。这种方法的优点是不需要显式初始化实例,如果使用不当,可能会导致混淆和错误
然而,相反地,无法明确设置种子,因此在测试或保存游戏状态或类似情况下,很难重现有用的结果。在这些情况下,可以使用下面所示的Java 1.7之前的技术
在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);
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;
}
看。在实践中,这门课往往比其他课程更可取
特别是,当标准库中有一个简单的API来完成任务时,无需重新发明随机整数生成轮。我想知道库提供的任何随机数生成方法是否符合要求
rand.nextInt((max+1) - min) + min;
例如:或
或者从中查看RandomUtils。您可以将第二个代码示例编辑为:
Random rn = new Random();
int range = maximum - minimum + 1;
int randomNum = rn.nextInt(range) + minimum;
请注意,这种方法比nextInt
方法更具偏见,效率更低
实现这一点的一个标准模式是:
Min + (int)(Math.random() * ((Max - Min) + 1))
数学库函数Math.random()在[0,1)
范围内生成一个双精度值。请注意,此范围不包括1
为了首先获得一个特定的值范围,需要乘以要覆盖的值范围的大小
Math.random() * ( Max - Min )
这将返回范围[0,Max-Min)
中的值,其中不包括“Max-Min”
例如,如果希望[5,10)
,则需要覆盖五个整数值,以便使用
Math.random() * 5
这将返回范围[0,5)
内的值,其中不包括5
现在,您需要将此范围向上移动到目标范围。您可以通过添加最小值来完成此操作
Min + (Math.random() * (Max - Min))
现在,您将获得范围[Min,Max)
的值。按照我们的示例,这意味着[5,10)
:
但是,这仍然不包括Max
,您将获得一个双精度值。为了获得包含的Max
值,您需要向范围参数(Max-Min)
中添加1,然后通过强制转换为整数来截断小数部分。这通过以下方式实现:
Min + (int)(Math.random() * ((Max - Min) + 1))
这是一个随机整数值,范围为[Min,Max]
,或者按照示例[5,10]
:
5 + (int)(Math.random() * ((10 - 5) + 1))
中的Math.Random
类是基于0的。因此,如果您编写以下内容:
Random rand = new Random();
int x = rand.nextInt(10);
x
将介于0-9
之间
因此,给定以下25
项的数组,生成0
(数组的基部)和array.length
之间的随机数的代码为:
String[] i = new String[25];
Random rand = new Random();
int index = 0;
index = rand.nextInt( i.length );
由于i.length
将返回25
,因此nextInt(i.length)
将返回一个介于0-24
范围内的数字。另一个选项与Math.Random
相同
index = (int) Math.floor(Math.random() * i.length);
要更好地理解,请查看论坛帖子。使用:
minimum + rn.nextInt(maxValue - minvalue + 1)
Random ran = new Random();
int x = ran.nextInt(6) + 5;
整数x
现在是一个随机数,其结果可能是5-10
如果掷骰子,它将是介于1到6(而不是0到6)之间的随机数,因此:
这很好。请原谅我的挑剔,但是大多数人建议的解决方案,即min+rng.nextInt(max-min+1))
,似乎很危险,因为:
rng.nextInt(n)
无法达到Integer.MAX\u值
(最大-最小)
当min
为负值时,可能会导致溢出
一个简单的解决方案将返回任何min-max)的正确结果{
抛出新的IllegalArgumentException(“无法从无效范围[“+min+”,“+max+”])中随机抽取int;
}
int diff=最大-最小值;
if(diff>=0&&diff!=Integer.MAX_值){
返回值(最小值+最小值+最小值(差值+1));
}
int i;
做{
i=rng.nextInt();
}而(imax);
返回i;
}
尽管效率低下,但请注意,while
循环中的成功概率始终为50%或更高。另一个选项只是使用:
public static Random RANDOM = new Random(System.nanoTime());
public static final float random(final float pMin, final float pMax) {
return pMin + RANDOM.nextFloat() * (pMax - pMin);
}
下面是一个有用的类,可以在包含/排除边界的任意组合范围内生成随机int
:
import java.util.Random;
public class RandomRange extends Random {
public int nextIncInc(int min, int max) {
return nextInt(max - min + 1) + min;
}
public int nextExcInc(int min, int max) {
return nextInt(max - min) + 1 + min;
}
public int nextExcExc(int min, int max) {
return nextInt(max - min - 1) + 1 + min;
}
public int nextIncExc(int min, int max) {
return nextInt(max - min) + min;
}
}
我发现这个例子:
此示例生成特定范围内的随机整数
import java.util.Random;
/** Generate random integers in a certain range. */
public final class RandomRange {
public static final void main(String... aArgs){
log("Generating random integers in the range 1..10.");
int START = 1;
int END = 10;
Random random = new Random();
for (int idx = 1; idx <= 10; ++idx){
showRandomInteger(START, END, random);
}
log("Done.");
}
private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
if ( aStart > aEnd ) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
int randomNumber = (int)(fraction + aStart);
log("Generated : " + randomNumber);
}
private static void log(String aMessage){
System.out.println(aMessage);
}
}
此方法可能便于使用:
此方法将返回一个介于提供的最小值和最大值之间的随机数:
public static int getRandomNumberBetween(int min, int max) {
Random foo = new Random();
int randomNumber = foo.nextInt(max - min) + min;
if (randomNumber == min) {
// Since the random number is between the min and max values, simply add 1
return min + 1;
} else {
return randomNumber;
}
}
此方法将从提供的最小值和最大值返回一个随机数(因此生成的数字也可以是最小值或最大值):
ThreadLocalRandom
相当于多线程环境中的类java.util.Random
。生成随机数是在每个线程的本地执行的。因此,通过减少冲突,我们可以获得更好的性能
int rand = ThreadLocalRandom.current().nextInt(x,y);
x
,y
-间隔,例如(1,10)只需执行以下语句即可:
Randomizer.generate(0,10);//最小值为零,最大值为十
下面是它的源代码
Randomizer.java
公共类随机化器{
公共静态int-generate(int-min,int-max){
返回min+(int)(Math.random()*((max-min)+1));
}
}
它只是简单明了。只需使用t
Random r = new Random();
int[] fiveRandomNumbers = r.ints(5, 0, 11).toArray();
int randomNumber = r.ints(1, 0, 11).findFirst().getAsInt();
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();
}
}
RandomUtils random = new RandomUtils();
random.nextInt(0, 0); // returns 0
random.nextInt(10, 10); // returns 10
random.nextInt(-10, 10); // returns numbers from -10 to 10 (-10, -9....9, 10)
random.nextInt(10, -10); // throws assert
import junit.framework.Assert;
import java.util.Random;
public class RandomUtils extends Random {
/**
* @param min generated value. Can't be > then max
* @param max generated value
* @return values in closed range [min, max].
*/
public int nextInt(int min, int max) {
Assert.assertFalse("min can't be > then max; values:[" + min + ", " + max + "]", min > max);
if (min == max) {
return max;
}
return nextInt(max - min + 1) + min;
}
}
Random rand = new Random();
randomNum = minimum + rand.nextInt((maximum - minimum) + 1);
public static int generateRandomInteger(int min, int max) {
SecureRandom rand = new SecureRandom();
rand.setSeed(new Date().getTime());
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
Random rn = new Random();
int result = rn.nextInt(max - min + 1) + min;
System.out.println(result);
/**
* @param min - The minimum.
* @param max - The maximum.
* @return A random double between these numbers (inclusive the minimum and maximum).
*/
public static double getRandom(double min, double max) {
return (Math.random() * (max + 1 - min)) + min;
}
Random random = new Random();
int max = 10;
int min = 5;
int totalNumber = 10;
IntStream stream = random.ints(totalNumber, min, max);
stream.forEach(System.out::println);
Random r = new Random();
int lowerBound = 1;
int upperBound = 11;
int result = r.nextInt(upperBound-lowerBound) + lowerBound;
int n = new SplittableRandom().nextInt(0, 1_001);
int[] a = new SplittableRandom().ints(100, 0, 1_001).parallel().toArray();
IntStream stream = new SplittableRandom().ints(100, 0, 1_001);