Use of Java random numbers

Source: Internet
Author: User

There are two classes of implementing random numbers in Java, respectively, and Java.util.Math and Java.util.Random

First type: Java.lang.Math.random ()

The Math.random () method creates a floating-point number for the [0.0,1.0] range class

Example code:

        int i = 0;        System.out.println ("math.random generates random numbers!") );          while (I < 3) {            System.out.println ("No." + i + "number:" + math.random ());            I+ +;        }

Console output:

The second kind: Java.util.Random class

The Java.util.Random class has two constructors, random () and random (long Seed)

The main difference between the two constructors is that seed is different, and seed is the variable required to implement the random class, that is, the random class creates the desired seed, and random generates a different sequence of random numbers based on it.

Random () uses the default seed value, and the random (long seed) implementation generates a sequence of corresponding random numbers based on the input seed value.

That is, the random object of the same seed is created with the same number sequence.

Example code:

        New Java.util.Random (ten);         New Java.util.Random (ten);         New java.util.Random ();        System.out.println ("random1:" +Random1.nextint ());        System.out.println ("random1_sanmeseed:" +Random1_sanmeseed.nextint ());        System.out.println ("random2:" +random2.nextint ());

Console output:

Note: When using the Nextint test, it is important to note that using a method with no arguments or parameters is the same as the seed value of random will also output a different random number.

This shows that the scope of application of the Java.util.Random class is more extensive, and then roughly speaking of its several methods, namely:

Nextint (); nextint (int bound); nextdouble ();

The difference between Nextint () nextint (int bound) is that bound limits the range of random numbers, and the random numbers generated using the latter method are generated between [0,bound] without parameters [- 2147483648,2147483647] The number of random numbers in the range, which is also the value range of int.

        New java.util.Random ();          for (int i=0;i<10;i++) {            System.out.println ("random number generated with bound parameter method:" +random.nextint ());            System.out.println ("random number generated by the parameterless method:     " +Random.nextint ());        }

Console output:

The random number returned by the Nextdouble () method is the same as the range of random numbers returned by Math.random (), which are all random numbers within [0,1].

So what happens to random decimals in the range of [ -3,12]? (The same problem applies to nextint (), nextfloat (), etc.)

        New java.util.Random ();          for (int i=0;i<10;i++) {            double num = random.nextdouble () *15-3;            double random number between System.out.println ("[ -3,12):" +num);        }

Console output:

Use of Java random numbers

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.