The random () function in Java

Source: Internet
Author: User
Tags rand

today in the Java practice to notice a random function in Java--random, just beginning to know that the function has a random value of the role, so the internet search for the data, and do some summary of the random function: There are two kinds of random functions in Java: first, java.lang.Math.Random;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, Evenly distributed within the range (approximate). For example, the following experimental codeafter compiling the pass, run the result as The observation will find that the code uses a loop 10 times to output num's value, all randomly distributed between [0,3]! When using Math.random (), the function is to return a value of type double, so be aware of the need for a shape conversion when assigning to other types of variables. second, java.util.Random;In the Java API help documentation, a summary of the function of this random () function is described:1. The random algorithm implemented in the Java.util.Random class is pseudo-random, that is, random with rules , the so-called rules are randomly generated numbers in the interval of a given seed (seed);2, the same seed number of random objects, the same number of times generated by the stochastic number is exactly the same;3. Random numbers generated by each method in the random class are evenly distributed, that is, the probability of the number generation within the interval is equal;two ways to construct the following random () 1.Random ():Creates a new random number generator. 2.Random (Long Seed):Creates a new random number generator with a single long seed.We can specify the seed when constructing the Random object (specify what the seed will do here, then look down), such as:random r1 = new random (+);or the default current system time corresponds to the relative time of the number as a seed number:random r1 = new Random ();It is necessary to note that when you create a random object you can give any number of valid seed numbers, the seed number is only the origin number of the random algorithm, and the interval of the generated random number has no relation . As in the following Java code:Random Rand =new random (+);int i;I=rand.nextint (+);Initialization 25 does not have a direct effect (note:it does not work), Rand.nextint (100), 100 is the upper limit of the random number, the resulting random number is a 0-100 integer, excluding 100. here is a summary of the Java.util.Random () method 1.protected int next (int bits): Generates the next pseudo-random number. 2.boolean Nextboolean (): Returns the next pseudo-random number, which is a Boolean value derived from the uniform distribution of this random number generator sequence. 3.void nextbytes (byte[] bytes): Generates a random byte and places it in a user-supplied byte array. 4.double nextdouble (): 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.float nextfloat (): 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.double Nextgaussian (): 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.int nextint (): Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator. 8.int nextint (int n): Returns a pseudo-random number, which is an int value that is taken from the sequence of this random number generator and is evenly distributed between (including and specified values (not included). 9.long Nextlong (): Returns the next pseudo-random number, which is a long value derived from the uniform distribution of this random number generator sequence. 10.void setseed (Long Seed): Sets the seed of this random number generator with a single long seed.  Here are a few examples of method summaries:1. Generate decimal for [0,1.0] interval: double D1 = r.nextdouble ();2. Generate decimal for [0,5.0] interval: Double d2 = r.nextdouble () * 5;3. Generate decimal for [1,2.5] interval: Double d3 = r.nextdouble () * 1.5 + 1;4. Generate an integer between 231 and 231-1: int n = r.nextint ();5. Generate an integer for the [0,10] interval:int n2 = R.nextint (10);//Method OneN2 = Math.Abs (r.nextint ()% 10);//Method TwoThe question of specifying the seed at the time of constructing the random object has been mentioned earlier, so what is the purpose of specifying the seed, which is illustrated here directly with the code example: 

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, let's compare the characteristics of these two random functions in a nutshell: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, a short time interval of 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.

The random () function in Java

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.