Java ThreadLocalRandom 类的 nextLong() 方法返回一个伪随机long值。此方法覆盖 Random 类中的 nextLong。
用法:
public long nextLong()
参数:
NA
返回值:
此方法返回一个伪随机long值。
示例
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample1 {
public static void main(String[] args) {
// Returns a pseudorandom long value
System.out.println("Random long value is:" +ThreadLocalRandom.current().nextLong());
}
}
输出:
Random long value is:8925561344935968546
Java ThreadLocalRandom nextLong(long bound) 方法
Java ThreadLocalRandom 类的 nextLong(long bound) 方法返回一个伪随机数。它返回 0 和边界之间的均匀分布值。
用法:
public long nextLong(long bound)
参数:
bound:它是随机数的界限。它必须是积极的。
返回值:
此方法返回下一个值。
异常:
IllegalArgumentException - 如果 n 不是正数,将抛出此异常。
例子1
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample2 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed long value between 0 and 2000
System.out.println("Random long value is:" + ThreadLocalRandom.current().nextLong(2000));
}
}
输出:
Random long value is:1703
例子2
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample3 {
public static void main(String[] args) {
// Returns exception because bound is negative
System.out.println("Random long value is:" +ThreadLocalRandom.current().nextInt(-10));
}
}
输出:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be positive at java.base/java.util.concurrent.ThreadLocalRandom.nextInt(Unknown Source) at tests.JavaNextLongExample2.main(ThreadLocalRandomNextLongExample2.java:7)
Java ThreadLocalRandom nextLong(long origin, long bound) 方法
Java ThreadLocalRandom 类的 nextLong(long origin, long bound) 方法返回一个伪随机数。它返回给定最小值和界限之间的均匀分布值。
用法:
public long nextLong(long origin, long bound)
参数:
origin- 这是最小的值。
bound- 它是上限(独占)。
返回值:
此方法返回原点和边界之间的伪随机long值。
异常:
IllegalArgumentException - 如果 origin 大于或等于 bound,将抛出此异常。
例子1
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample4 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed long value between 10000 and 50000
System.out.println("Random long value is:" + ThreadLocalRandom.current().nextLong(10000, 50000));
}
}
输出:
Random long value is:39171
例子2
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample5 {
public static void main(String[] args) {
// Returns a pseudorandom, uniformly distributed long value between 26000 and 60000
System.out.println("Random long value is:" + ThreadLocalRandom.current().nextLong(26000, 60000));
}
}
输出:
Random long value is:42248
例子3
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomNextLongExample6 {
public static void main(String[] args) {
// Returns exception because the bound is negative
System.out.println("Random long value is:" + ThreadLocalRandom.current().nextLong(-10000));
}
}
输出:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be positive at java.base/java.util.concurrent.ThreadLocalRandom.nextLong(Unknown Source) at tests.JavaNextLongExample2.main(ThreadLocalRandomNextLongExample3.java:7)
相关用法
- Java ThreadLocalRandom nextDouble()用法及代码示例
- Java ThreadLocalRandom nextGaussian()用法及代码示例
- Java ThreadLocalRandom nextInt()用法及代码示例
- 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 nextLong() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。