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


Java Java.util.Random用法及代碼示例


java中Random類用於生成偽隨機數。此類的實例是線程安全的。然而,此類的實例在加密上是不安全的。該類提供了各種方法調用來生成不同的隨機數據類型,如float、double、int。

構造函數:

  • Random():創建一個新的隨機數生成器
  • 隨機(長種子):使用單個長種子創建一個新的隨機數生成器

聲明:

public class Random
               extends Object
               implements Serializable

方法:

  1. java.util.Random.doubles():返回實際上無限的偽隨機雙精度值流,每個值都在零(包含)和一(不包含)之間
    用法:
    public DoubleStream doubles()
    返回:
    a stream of pseudorandom double values
  2. java.util.Random.ints():返回有效無限的偽隨機 int 值流
    用法:
    public IntStream ints()
    Returns:
    a stream of pseudorandom int values
  3. java.util.Random.longs():返回實際上無限的偽隨機long值流
    用法:
    public LongStream longs()
    Returns:
    a stream of pseudorandom long values
  4. 下一個(整數位):java.util.Random.next(整數位)生成下一個偽隨機數
    用法:
    protected int next(int bits)
    Parameters:
    bits - random bits
    返回:
    the next pseudo random value from this 
    random number generator's sequence
  5. java.util.Random.nextBoolean():返回此隨機數生成器序列中的下一個偽隨機、均勻分布的布爾值
    用法:
    public boolean nextBoolean()
    Returns:
    the next pseudorandom, uniformly distributed boolean value 
    from this random number generator's sequence
  6. java.util.Random.nextBytes(byte[] 字節):生成隨機字節並將它們放入用戶提供的字節數組中
    用法:
    public void nextBytes(byte[] bytes)
    Parameters:
    bytes - the byte array to fill with random bytes
    Throws:
    NullPointerException - if the byte array is null
  7. java.util.Random.nextDouble():返回此隨機數生成器序列中的下一個偽隨機、均勻分布的 0.0 到 1.0 之間的雙精度值
    用法:
    public double nextDouble()
    Returns:
    the next pseudo random, uniformly distributed double 
    value between 0.0 and 1.0 from this 
    random number generator's sequence
  8. java.util.Random.nextFloat():返回此隨機數生成器序列中的下一個偽隨機、均勻分布的浮點值(介於 0.0 和 1.0 之間)
    用法:
    public float nextFloat()
    Returns:
    the next pseudorandom, uniformly distributed float value 
    between 0.0 and 1.0 from this 
    random number generator's sequence
  9. java.util.Random.nextGaussian():返回此隨機數生成器序列中的下一個偽隨機、高斯 (“normally”) 分布雙精度值,平均值為 0.0,標準差為 1.0
    用法:
    public double nextGaussian()
    Returns:
    the next pseudorandom, Gaussian ("normally") distributed double
    value with mean 0.0 and standard deviation 1.0 from this 
    random number generator's sequence
  10. java.util.Random.nextInt()返回此隨機數生成器序列中的下一個偽隨機、均勻分布的 int 值
    用法:
    public int nextInt()
    Returns:
    the next pseudorandom, uniformly distributed int value from 
    this random number generator's sequence
  11. java.util.Random.nextInt(int綁定):返回從該隨機數生成器的序列中提取的介於 0(含)和指定值(不含)之間的偽隨機、均勻分布的 int 值
    用法:
    public int nextInt(int bound)
    Parameters:
    bound - the upper bound (exclusive). Must be positive.
    返回:
    the next pseudorandom, uniformly distributed int value 
    between zero (inclusive) and bound 
    (exclusive) from this random number generator's sequence
    Throws:
    IllegalArgumentException - if bound is not positive
  12. java.util.Random.nextLong():返回此隨機數生成器序列中的下一個偽隨機、均勻分布的long值
    用法:
    public long nextLong()
    Returns:
    the next pseudorandom, uniformly distributed long value
    from this random number 
    generator's sequence
  13. java.util.Random.setSeed(長種子):使用單個長種子設置此隨機數生成器的種子
    用法:
    public void setSeed(long seed)
    Parameters:
    seed - the initial seed

Methods inherited from class java.lang.Object

  • clone
  • equals
  • finalize
  • getClass
  • hashCode
  • notify
  • notifyAll
  • toString
  • wait

Java program to demonstrate usage of Random class


// Java program to demonstrate 
// method calls of Random class 
import java.util.Random; 
  
public class Test 
{ 
    public static void main(String[] args) 
    { 
        Random random = new Random(); 
        System.out.println(random.nextInt(10)); 
        System.out.println(random.nextBoolean()); 
        System.out.println(random.nextDouble()); 
        System.out.println(random.nextFloat()); 
        System.out.println(random.nextGaussian()); 
        byte[] bytes = new byte[10]; 
        random.nextBytes(bytes); 
        System.out.printf("["); 
        for(int i = 0; i< bytes.length; i++) 
        { 
            System.out.printf("%d ", bytes[i]); 
        } 
        System.out.printf("]\n"); 
          
          System.out.println(random.nextLong());   
      System.out.println(random.nextInt()); 
       
      long seed = 95; 
      random.setSeed(seed); 
        
      // Note: Running any of the code lines below 
      // will keep the program running as each of the  
      // methods below produce an unlimited random  
      // values of the corresponding type 
  
      /* System.out.println("Sum of all the elements in the IntStream returned = " +  
        random.ints().count()); 
      System.out.println("Count of all the elements in the DoubleStream returned = " +  
        random.doubles().count()); 
      System.out.println("Count of all the elements in the LongStream returned = " +  
        random.longs().count()); 
       
      */
     
  } 
}      

輸出:

4
true
0.19674934340402916
0.7372021
1.4877581394085997
[-44 75 68 89 81 -72 -1 -66 -64 117 ]
158739962004803677
-1344764816

參考:



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.util.Random class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。