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


Java ThreadLocalRandom nextLong()用法及代碼示例

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