The use case analysis of Java Common class-random class

Source: Internet
Author: User
Tags generator

This article introduces the use of the random class of Java common classes, the following is the actual case, no additional text explanation, need to explain I have written in the code comments.

Import Java.util.random;public class Random1 {public static void main (string[] args) {//Two constructors of Random R1        =new Random (); Random r2=new random (120);        Creates a new random number generator with a single long seed.        System.out.println (R1.next (2)); The return value is the protection type//SYSTEM.OUT.PRINTLN (R2.next (2));        System.out.println (R1.nextboolean ());//Returns the next pseudo-random number, which is a Boolean value derived from the uniform distribution of this random number generator sequence.        System.out.println (R1.nextboolean ());        System.out.println ("+++++++++++");        Byte[] By1=new byte[5];        Byte[] By2=new byte[5];        R1.nextbytes (by1);//Generate random bytes and place them in a user-supplied byte array.        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 ("+++++++++++");        Returns the next pseudo-random number, which is a double value that is evenly distributed between 0.0 and 1.0, taken from this random number generator sequence.      System.out.println (R1.nextdouble ());  System.out.println (R1.nextdouble ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a float value that is evenly distributed between 0.0 and 1.0, taken from this random number generator sequence.        System.out.println (R1.nextfloat ());        System.out.println (R1.nextfloat ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a double value of Gaussian ("normal") distribution taken from this random number generator sequence, with an average of 0.0 and a standard deviation of 1.0.        System.out.println (R1.nextgaussian ());        System.out.println (R1.nextgaussian ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.        System.out.println (R1.nextint ());        System.out.println (R1.nextint ());        System.out.println ("+++++++++++");        Returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (not included) from this random number generator sequence.        System.out.println (R1.nextint (100));        System.out.println (R1.nextint (50));        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a Long value System.out.println (R1.nextlong ()) derived from the uniform distribution of this random number generator sequence;        System.out.println (R1.nextlong ()); R1.setseed (10);A single long seed sets the seed of this random number generator. }}

Operation Result:

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+++++++++++ 32950749682653914961387264859162260419[finished in 1.2s]
Import Java.util.random;public class Random1 {public static void main (string[] args) {//Two constructors of Random R1        =new Random (); Random r2=new random (120);        Creates a new random number generator with a single long seed.        System.out.println (R1.next (2)); The return value is the protection type//SYSTEM.OUT.PRINTLN (R2.next (2));        System.out.println (R1.nextboolean ());//Returns the next pseudo-random number, which is a Boolean value derived from the uniform distribution of this random number generator sequence.        System.out.println (R1.nextboolean ());        System.out.println ("+++++++++++");        Byte[] By1=new byte[5];        Byte[] By2=new byte[5];        R1.nextbytes (by1);//Generate random bytes and place them in a user-supplied byte array.        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 ("+++++++++++");        Returns the next pseudo-random number, which is a double value that is evenly distributed between 0.0 and 1.0, taken from this random number generator sequence.      System.out.println (R1.nextdouble ());  System.out.println (R1.nextdouble ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a float value that is evenly distributed between 0.0 and 1.0, taken from this random number generator sequence.        System.out.println (R1.nextfloat ());        System.out.println (R1.nextfloat ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a double value of Gaussian ("normal") distribution taken from this random number generator sequence, with an average of 0.0 and a standard deviation of 1.0.        System.out.println (R1.nextgaussian ());        System.out.println (R1.nextgaussian ());        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.        System.out.println (R1.nextint ());        System.out.println (R1.nextint ());        System.out.println ("+++++++++++");        Returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (not included) from this random number generator sequence.        System.out.println (R1.nextint (100));        System.out.println (R1.nextint (50));        System.out.println ("+++++++++++");        Returns the next pseudo-random number, which is a Long value System.out.println (R1.nextlong ()) derived from the uniform distribution of this random number generator sequence;        System.out.println (R1.nextlong ()); R1.setseed (10);A single long seed sets the seed of this random number generator. }}

Operation Result:

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+++++++++++ 32950749682653914961387264859162260419[finished in 1.2s]

Related articles:

JAVA random class, array learning

Common classes that must be known in Java

Related videos:

Full parsing of Java annotations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.