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


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

Java ThreadLocalRandom 類的 longs() 方法返回一個有效無限的偽隨機long值流。此方法覆蓋 Random 類中的 long。

用法:

public LongStream longs()

參數:

NA

返回值:

此方法返回偽隨機long值流。

示例

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomLongsExample1 {
    public static void main(String[] args) {
    	
    	// Returns a stream of pseudorandom long values   	
    	System.out.println("stream of pseudorandom long value is:" +ThreadLocalRandom.current().longs());
    }
}

輸出:

stream of pseudorandom long value is:[email protected]

Java ThreadLocalRandom longs(long streamSize) 方法

Java ThreadLocalRandom 類的 longs(long streamSize) 方法返回一個流,該流產生給定的 streamSize 數量的偽隨機long值。此方法覆蓋 Random 類中的 long。

用法:

public LongStream longs(long streamSize)

參數:

streamSize- 它是要生成的值的數量

返回值:

此方法返回一個long值流。

異常:

IllegalArgumentException:如果 streamSize 小於零,將拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;  
  
public class ThreadLocalRandomLongsExample4 {  
    public static void main(String[] args) {  
          
        // Returns a stream of longs values      
        System.out.println("stream of longs value is:" +ThreadLocalRandom.current().longs(1500));  
    }  
}

輸出:

stream of longs value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;  
  
public class ThreadLocalRandomLongsExample5 {  
    public static void main(String[] args) {  
          
        // Returns a stream of longs values      
    	System.out.println("stream of long value is:" +ThreadLocalRandom.current().longs(-20));  
    }  
}

輸出:

Exception in thread "main" java.lang.IllegalArgumentException:size must be non-negative
	at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source)
	at tests.ThreadLocalRandomLongsExample1.main(ThreadLocalRandomLongsExample2.java:7)

Java ThreadLocalRandom longs(long randomNumberOrigin, long randomNumberBound) 方法

Java ThreadLocalRandom 類的 longs(long randomNumberOrigin, long randomNumberBound) 方法返回一個有效無限的偽隨機long值流。每個值必須符合給定的原點(包含)和邊界(不包含)。此方法覆蓋 Random 類中的 long。

用法:

public LongStream longs(long randomNumberOrigin, long randomNumberBound)

參數:

randomNumberOrigin:它是每個隨機值的原點(含)。

randomNumberBound:它是每個隨機值的界限(不包括)。

返回值:

此方法返回偽隨機long值流。

異常:

IllegalArgumentException:如果 randomNumberOrigin 大於或等於 randomNumberBound,則會拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomLongsExample2 {
    public static void main(String[] args) {
    	
    	// Returns a stream of pseudorandom long values   	
    	System.out.println("stream of pseudorandom long value is:" +ThreadLocalRandom.current().longs(20, 30));
    }
}

輸出:

stream of pseudorandom long value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomLongsExample3 {
    public static void main(String[] args) {
    	
    	// Returns exception because origin is greater than bound  	
    	System.out.println("stream of pseudorandom long value is:" +ThreadLocalRandom.current().longs(30, 10));
    }
}

輸出:

Exception in thread "main" java.lang.IllegalArgumentException:bound must be greater than origin
	at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source)
	at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample2.java:7)

Java ThreadLocalRandom longs(long streamSize, long randomNumberOrigin, long randomNumberBound) 方法

Java ThreadLocalRandom 類的 longs(long streamSize, long randomNumberOrigin, long randomNumberBound) 方法返回一個流,該流產生給定的 streamSize 數量的偽隨機long值。每個值必須符合給定的原點(包含)和邊界(不包含)。此方法覆蓋 Random 類中的 long。

用法:

public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)

參數:

streamSize- 它是要生成的值的數量

randomNumberOrigin- 是每個隨機值的原點(含)

randomNumberBound- 它是每個隨機值的界限(不包括)

返回值:

此方法返回偽隨機long值流。

異常:

  1. IllegalArgumentException: 如果 streamSize 小於零,將拋出此異常。
  2. IllegalArgumentException: 如果 randomNumberOrigin 大於或等於 randomNumberBound,則會拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;  
  
public class ThreadLocalRandomLongsExample6 {  
    public static void main(String[] args) {  
          
        // Returns a stream of pseudorandom long values         
        System.out.println("stream of pseudorandom long value is:" +ThreadLocalRandom.current().longs(10, 20, 30));  
    }  
}

輸出:

stream of pseudorandom long value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;  
  
public class ThreadLocalRandomLongsExample7 {  
    public static void main(String[] args) {  
          
        // Returns exception because steamSize is negative        
        System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles(-10, 20, 30));  
    }  
}

輸出:

Exception in thread "main" java.lang.IllegalArgumentException:size must be non-negative
	at java.base/java.util.concurrent.ThreadLocalRandom.doubles(Unknown Source)
	at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample1.java:7)






相關用法


注:本文由純淨天空篩選整理自 Java ThreadLocalRandom longs() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。