Java generates random numbers

Source: Internet
Author: User

Two common methods of generating random numbers in Java the Random () method in the Java.lang.Math class;

Call this math.random () function to return a double with a positive sign, which is greater than or equal to 0.0 and less than 1.0, that is, the value range is [0.0,1.0] the left closed right open interval, the return value is a pseudo-random selection of the number, within that range (approximate) evenly distributed.

The first time the method is called, it creates a new pseudo-random number generator that is exactly the same as the following expression

New Java.util.Random
After that, the new pseudo-random number generator can be used for all calls to this method, but not elsewhere.

This method is fully synchronized and allows multiple threads to be used without errors. However, if many threads need to generate pseudo-random numbers at very high rates, this may reduce the contention that each thread has its own pseudo-random number generator.

Second, the java.util.Random category;1. The random algorithm implemented in the Java.util.Random class is pseudo-random, which is there are rules .Random, the so-called rule is to randomly generate numbers in the interval of a given seed (seed), 2, the same number of random objects, the same number of randomly generated numbers are exactly the same; 3. Random numbers generated by each method in the random class are evenly distributed, That is, the probability of generating the numbers within the interval is equal; The following two methods of constructing random () 1.Random ():Creates a new random number generator. 2.Random (Long Seed):Creates a new random number generator with a single long seed.
// gets the number of milliseconds of the current time as a random number seed long t = System.currenttimemillis ();
1.protected intNextintBITS)://generates the next pseudo-random number. 2.BooleanNextboolean ()://returns the next pseudo-random number, which is a Boolean value derived from the uniform distribution of this random number generator sequence. 3.voidNextbytes (byte[] bytes)://generates a random byte and places it in a user-supplied byte array. 4.DoubleNextdouble ()://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. 5.floatNextfloat ()://returns the next pseudo-random number, which is taken from this random number generator sequence and evenly distributes the float value between 0.0 and 1.0. 6.DoubleNextgaussian ()://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 standard deviations of 1.0. 7.intNextint ()://returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator. 8.intNextint (intN)://returns a pseudo-random number, which is an int value that is taken from the sequence of this random number generator, evenly distributed between (including and specified values, not included). 9.LongNextlong ()://returns the next pseudo-random number, which is a long value derived from the uniform distribution of this random number generator sequence. 10.voidSetseed (LongSeed)://use a single long seed to set the seed for this random number generator. 
Here are a few examples of the method summary: 1. The decimal number of the generation [0,1.0] interval: double D1 = r.nextdouble (); 2. Decimal of the generation [0,5.0] interval: Double d2 = r.nextdouble () * 5;3. Build [ 1,2.5) The fractional number of intervals: double d3 = r.nextdouble () * 1.5 + 1;4. Generates an integer between 231 and 231-1: int n = r.nextint (); 5. Integer that generates the [0,10] interval: int n2 = R.nextin T (10);//Method One N2 = Math.Abs (r.nextint ()% 10);//Method two in front of the construction of the random object when the problem of the specified seed, exactly what the role of the specified seed, here directly with the code example to illustrate:

At the time of the definition of the same seed, respectively, using R1 and R2 to go to [0,30) random number, the results of compilation execution regret found that the results are presented AABB type, indicating that R1 and R2 take the random number is identical (for the experiment). If I change the code, change it to the following:

After compiling the output, we will no longer get the result of the AABB type, according to the difference between the code, you can know the number of the specified seed, and the difference between the number of unspecified seed is where. Finally, a simple comparison of the characteristics of these two random functions: 1.java.math.random () is actually called internally Java.util.Random (), it has a fatal weakness, it is related to the system time, That is to say, the time is very short two random such as: double A = Math.random (), double b = math.random (), it is possible to get two identical double. 2.java.util.random () can be implemented at the time of invocation and Java. Math.random () The same function, and he has a lot of calling methods, relatively flexible. So, in general, using Java.util.Random () is relatively more flexible. Reference:https://www.cnblogs.com/uncle-box/p/5918743.html

Java generates random numbers

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.