當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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