📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:38.624000             🧑  作者: Mango

Java Java类

Random 类用于在Java中生成伪随机数。此类的实例是线程安全的。然而,这个类的实例在密码学上是不安全的。该类提供各种方法调用来生成不同的随机数据类型,如float、double、int。

构造函数:

  • Random() : 创建一个新的随机数生成器
  • Random(long seed) :使用单个长种子创建一个新的随机数生成器

宣言:

public class Random
               extends Object
               implements Serializable

方法:

  1. Java.util.Random.doubles():返回一个有效无限的伪随机双精度值流,每个值在零(包括)和一(不包括)之间
    句法:
    public DoubleStream doubles()
    Returns:
    a stream of pseudorandom double values
  2. Java.util.Random.ints():返回一个有效无限的伪随机整数流
    句法:
    public IntStream ints()
    Returns:
    a stream of pseudorandom int values
  3. Java.util.Random.longs():返回一个有效无限的伪随机长值流
    句法:
    public LongStream longs()
    Returns:
    a stream of pseudorandom long values
  4. next(int bits): Java.util.Random.next(int bits)生成下一个伪随机数
    句法:
    protected int next(int bits)
    Parameters:
    bits - random bits
    Returns:
    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[] bytes) :生成随机字节并将它们放入用户提供的字节数组中
    句法:
    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():从这个随机数生成器的序列返回下一个伪随机、高斯(“正常”)分布的双精度值,均值为 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 bound):返回一个伪随机的、均匀分布的 int 值,介于 0(包括)和指定值(不包括)之间,取自此随机数生成器的序列
    句法:
    public int nextInt(int bound)
    Parameters:
    bound - the upper bound (exclusive). Must be positive.
    Returns:
    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():从这个随机数生成器的序列中返回下一个伪随机、均匀分布的长值
    句法:
    public long nextLong()
    Returns:
    the next pseudorandom, uniformly distributed long value
    from this random number 
    generator's sequence
  13. Java.util.Random.setSeed(long seed):使用单个长种子设置此随机数生成器的种子
    句法:
    public void setSeed(long seed)
    Parameters:
    seed - the initial seed

从类Java.lang.Object 继承的方法

  • 克隆
  • 等于
  • 敲定
  • 获取类
  • 哈希码
  • 通知
  • 通知所有
  • 到字符串
  • 等待

演示 Random 类用法的Java程序

// 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

参考:

  • 甲骨文