Java random number

Source: Internet
Author: User
Tags generator time in milliseconds

1 three ways to generate random numbers

This chapter explains several ways to generate Java random numbers, and then demonstrates them by example.

Broadly speaking, there are three ways to generate random numbers in Java:

(01). Gets a long number with a current time of milliseconds by System.currenttimemillis ().
(02). Returns a double value between 0 and 1 via math.random ().
(03). A random number is generated by the random class, which is a professional random tool class with powerful functions.

1.1 The 1th kind

Gets the random number by System.currenttimemillis (). Actually gets the current time in milliseconds, which is a long type. Here's how to use it:

Final long L = System.currenttimemillis ();

To get an integer of type int, simply turn the above result into an int type. For example, get an int integer between [0, 100]. Here's how:

Final long L = system.currenttimemillis (); Final int i = (int) (l% 100);
1.2 The 2nd kind

Gets the random number by Math.random (). In fact, it returns a double value between 0 (inclusive) and 1 (not included). Here's how to use it:

Final double d = math.random ();

To get an integer of type int, simply turn the above result into an int type. For example, get an int integer between [0, 100]. Here's how:

Final double d = math.random (); Final int i = (int) (d*100);
1.3 The 3rd kind

Gets the random number through the random class.

Here's how to use it:
create a Random object. There are two ways to create a random object, as follows:

New Random (); // default constructor Method New Random (1000); // Specify seed numbers

To get the random number by the random object. random value types supported by Random include: Boolean, Byte, int, long, float, double.
For example, get an int integer between [0, 100]. Methods are as follows

int i2 = random.nextint (100);

function interface for Random

//Constructor (i): Creates a new random number generator. Random ()//Constructor (ii): Creates a new random number generator with a single long seed: public random (Long Seed) {setseed (seed);} The next method uses it to hold the state of the random number generator. Random (Longseed)BooleanNextboolean ()//returns the next "Boolean type" pseudo-random number. voidNextbytes (byte[] buf)//generates a random byte and places it in the byte array buf. DoubleNextdouble ()//returns the random number of a double type between "[0.0, 1.0)". floatNextfloat ()//returns a random number of type "float" between [0.0, 1.0). intNextint ()//returns the next "int type" random number. intNextint (intN//returns a random number of type int "[0, N)". LongNextlong ()//returns the next "long" random number. synchronized DoubleNextgaussian ()//returns the random number of the next double type, which is a double value with a Gaussian ("normal") distribution, with an average of 0.0 and a standard deviation of 1.0. synchronized voidSetseed (LongSeed//use a single long seed to set the seed for this random number generator. 

1.4 Example of getting random numbers

The following example shows the above 3 ways to get a random number.
The source code is as follows (Randomtest.java):

ImportJava.util.Random;ImportJava.lang.Math;/*** Java random number test program. A total of 3 ways to get a random number: * (01), through System.currenttimemillis () to get a long number of milliseconds of the current time. * (02), returns a double value between 0 and 1 through Math.random (). * (03), through the random class to produce a random number, this is a professional random tool class, powerful. * * @authorSkywang * @email [email protected]*/ Public classrandomtest{ Public Static voidMain (String args[]) {//returns a random number through System Currenttimemillis ()Testsystemtimemillis (); //return random numbers by Math's random ()Testmathrandom (); //Create a new random object with seed 1000 and test the random API with that seedTestrandomapis (NewRandom (+), "1st random (1000)"); Testrandomapis (NewRandom (+), "2nd random (1000)"); //Create a new "default seed" random object and pass the seed to test the random APITestrandomapis (NewRandom (), "1st random ()"); Testrandomapis (NewRandom (), "2nd random ()"); }    /*** return random number-01: Test System Currenttimemillis ()*/    Private Static voidTestsystemtimemillis () {//through        Final LongL =System.currenttimemillis (); //get an integer between [0, 100] via L        Final inti = (int) (l% 100 ); System.out.printf ("\ n----system.currenttimemillis ()----\ l=%s i=%s\n", L, i); }    /*** return random number-02: Test math's random ()*/    Private Static voidTestmathrandom () {//returns a double type random number, range [0.0, 1.0) through Math's random () function        Final DoubleD =Math.random (); //get an integer between [0, 100] through D        Final inti = (int) (d*100); System.out.printf ("\ n----math.random ()----\ d=%s i=%s\n", D, i); }    /*** return random number-03: Test the Random API*/    Private Static voidTestrandomapis (random random, String title) {Final intBuffer_len = 5; //get a random boolean value        Booleanb =Random.nextboolean (); //get a random array of buf[]        byte[] buf =New byte[Buffer_len];        Random.nextbytes (BUF); //gets a random double value, range [0.0, 1.0)        DoubleD =random.nextdouble (); //get random float value, range [0.0, 1.0]        floatf =random.nextfloat (); //get a random int value        intI1 =Random.nextint (); //gets the int value between the random [0,100]        intI2 = Random.nextint (100); //gets the double value of the random Gaussian distribution        Doubleg =Random.nextgaussian (); //get a random Long value        LongL =Random.nextlong (); System.out.printf ("\ n----%s----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[", title, B, D, F, I1, I2, G, L);  for(bytebt:buf) System.out.printf ("%s,", BT); System.out.println ("]"); }}

(RPM) Java random number

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.