当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ThreadLocalRandom nextInt()用法及代码示例


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 nextInt() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。