Home  >  Article  >  Java  >  Java common classes-Usage case analysis of Random class

Java common classes-Usage case analysis of Random class

php是最好的语言
php是最好的语言Original
2018-08-06 11:23:421919browse

This article introduces the usage of the Random class, a commonly used Java class. The following is a practical case. There is no additional text explanation. I have written everything that needs to be explained in the code comments.

import java.util.Random;public class random1 {
    public static void main(String[] args){
        //两种构造函数
        Random r1=new Random();
        //Random r2=new Random(120); 使用单个 long 种子创建一个新的随机数生成器。

        //System.out.println(r1.next(2));返回值为保护类型
        //System.out.println(r2.next(2));
        System.out.println(r1.nextBoolean());//返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
        System.out.println(r1.nextBoolean());
        System.out.println("+++++++++++");
        byte[] by1=new byte[5];
        byte[] by2=new byte[5];
        r1.nextBytes(by1);//生成随机字节并将其置于用户提供的 byte 数组中。
        r1.nextBytes(by2);
        for(int i=0;i<by1.length;i++){
            System.out.print(by1[i]+" ");
        }
        System.out.println();
        for(int j=0;j<by2.length;j++){
            System.out.print(by2[j]+" ");
        }
        System.out.println();
        System.out.println("+++++++++++");

        //返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值。
        System.out.println(r1.nextDouble());
        System.out.println(r1.nextDouble());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值。
        System.out.println(r1.nextFloat());
        System.out.println(r1.nextFloat());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的 double 值,其平均值是 0.0,标准差是 1.0。
        System.out.println(r1.nextGaussian());
        System.out.println(r1.nextGaussian());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
        System.out.println(r1.nextInt());
        System.out.println(r1.nextInt());
        System.out.println("+++++++++++");
        //返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
        System.out.println(r1.nextInt(100));
        System.out.println(r1.nextInt(50));
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值
        System.out.println(r1.nextLong());
        System.out.println(r1.nextLong());

        //r1.setSeed(10);使用单个 long 种子设置此随机数生成器的种子。


    }
}

Running results:

Compiling random1.java.......
-----------OUTPUT-----------
falsefalse
+++++++++++-55 -112 41 -78 93 
54 127 -93 -22 120 
+++++++++++0.69656134440266490.06445584272260563
+++++++++++0.285779950.8657566
+++++++++++-0.15436582029316171.4500847476555192
+++++++++++1824132073-436413982
+++++++++++2921
+++++++++++3295074968265391496
1387264859162260419[Finished in 1.2s]
import java.util.Random;public class random1 {
    public static void main(String[] args){
        //两种构造函数
        Random r1=new Random();
        //Random r2=new Random(120); 使用单个 long 种子创建一个新的随机数生成器。

        //System.out.println(r1.next(2));返回值为保护类型
        //System.out.println(r2.next(2));
        System.out.println(r1.nextBoolean());//返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
        System.out.println(r1.nextBoolean());
        System.out.println("+++++++++++");
        byte[] by1=new byte[5];
        byte[] by2=new byte[5];
        r1.nextBytes(by1);//生成随机字节并将其置于用户提供的 byte 数组中。
        r1.nextBytes(by2);
        for(int i=0;i<by1.length;i++){
            System.out.print(by1[i]+" ");
        }
        System.out.println();
        for(int j=0;j<by2.length;j++){
            System.out.print(by2[j]+" ");
        }
        System.out.println();
        System.out.println("+++++++++++");

        //返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值。
        System.out.println(r1.nextDouble());
        System.out.println(r1.nextDouble());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值。
        System.out.println(r1.nextFloat());
        System.out.println(r1.nextFloat());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的 double 值,其平均值是 0.0,标准差是 1.0。
        System.out.println(r1.nextGaussian());
        System.out.println(r1.nextGaussian());
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
        System.out.println(r1.nextInt());
        System.out.println(r1.nextInt());
        System.out.println("+++++++++++");
        //返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
        System.out.println(r1.nextInt(100));
        System.out.println(r1.nextInt(50));
        System.out.println("+++++++++++");
        //返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值
        System.out.println(r1.nextLong());
        System.out.println(r1.nextLong());

        //r1.setSeed(10);使用单个 long 种子设置此随机数生成器的种子。


    }
}

Running results:

Compiling random1.java.......
-----------OUTPUT-----------
falsefalse
+++++++++++-55 -112 41 -78 93 
54 127 -93 -22 120 
+++++++++++0.69656134440266490.06445584272260563
+++++++++++0.285779950.8657566
+++++++++++-0.15436582029316171.4500847476555192
+++++++++++1824132073-436413982
+++++++++++2921
+++++++++++3295074968265391496
1387264859162260419[Finished in 1.2s]

Related articles:

JAVA Random class, array learning

Common classes that must be understood in java

Related videos:

Comprehensive analysis of Java annotations

The above is the detailed content of Java common classes-Usage case analysis of Random class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn