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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。