Java ThreadLocalRandom 类的 nextInt() 方法返回一个伪随机 int 值。此方法覆盖 Random 类中的 nextInt。
用法:
public int nextInt()
参数:
NA
返回值:
此方法返回一个伪随机 int 值。
例子1
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample1 {
public static void main(String[] args) {
// Returns a pseudorandom int value
System.out.println("Random int value is:" +ThreadLocalRandom.current().nextInt());
}
}
输出:
Random int value is:-2035038202
Java ThreadLocalRandom nextInt(int bound) 方法
Java ThreadLocalRandom 类的 nextInt(int bound) 方法返回一个介于零和指定界限之间的伪随机 int 值。此方法覆盖 Random 类中的 nextInt。
用法:
public int nextInt(int bound)
参数:
bound: 这是上限。它必须是积极的。
返回值:
此方法返回零和指定界限之间的伪随机整数值。
异常:
IllegalArgumentException:如果边界不是正数,将抛出此异常
例子1
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample2 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed integer value between 0 and 4000
System.out.println("Random int value is:" +ThreadLocalRandom.current().nextInt(4000));
}
}
输出:
Random int value is:1566
例子2
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample3 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed integer value between 0 and 38000
System.out.println("Random int value is:" +ThreadLocalRandom.current().nextInt(38000));
}
}
输出:
Random int value is:25266
例子3
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample4 {
public static void main(String[] args) {
// Returns exception because the bound is negative
System.out.println("Random int value is:" +ThreadLocalRandom.current().nextInt(-100));
}
}
输出:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be positive at java.base/java.util.concurrent.ThreadLocalRandom.nextInt(Unknown Source) at tests.JavaNextIntExample3.main(ThreadLocalRandomNextIntExample3.java:7)
Java ThreadLocalRandom nextInt(int least, int bound) 方法
Java ThreadLocalRandom 类的 nextInt(int least, int bound) 方法返回一个伪随机数。它返回给定最小值和界限之间的均匀分布值。
用法:
public int nextInt(int least, int bound)
参数:
least- 这是最小的值。
bound- 它是上限(独占)。
返回值:
此方法返回下一个值。
异常:
IllegalArgumentException - 如果最小值大于或等于 bound,将抛出此异常。
例子1
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample5 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed integer value between 10 and 20
System.out.println("Random int value is:" + ThreadLocalRandom.current().nextInt(10, 20));
}
}
输出:
Random int value is:11
例子2
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextIntExample6 {
public static void main(String[] args) {
// Returns exception because least is greater than bound
System.out.println("Random int value is:" +ThreadLocalRandom.current().nextInt(30, 10));
}
}
输出:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be greater than origin at java.base/java.util.concurrent.ThreadLocalRandom.nextInt(Unknown Source) at tests.JavaNextIntExample2.main(ThreadLocalRandomNextIntExample2.java:7)
相关用法
- Java ThreadLocalRandom nextLong()用法及代码示例
- Java ThreadLocalRandom nextDouble()用法及代码示例
- Java ThreadLocalRandom nextGaussian()用法及代码示例
- Java ThreadLocalRandom longs()用法及代码示例
- Java ThreadLocalRandom current()用法及代码示例
- Java ThreadLocalRandom ints()用法及代码示例
- Java ThreadLocalRandom setSeed()用法及代码示例
- Java ThreadLocalRandom doubles()用法及代码示例
- Java Thread toString()用法及代码示例
- Java ThreadGroup enumerate()用法及代码示例
- Java ThreadGroup getMaxPriority()用法及代码示例
- Java ThreadGroup getParent()用法及代码示例
- Java Thread interrupted()用法及代码示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代码示例
- Java Thread suspend()用法及代码示例
- Java Thread destroy()用法及代码示例
- Java Thread holdLock()用法及代码示例
- Java Thread getContextClassLoader()用法及代码示例
- Java Thread setContextClassLoader()用法及代码示例
- Java ThreadGroup getName()用法及代码示例
注:本文由纯净天空筛选整理自 Java ThreadLocalRandom nextInt() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。