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


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