The random () function in Java

Source: Internet
Author: User

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). 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. Example:
1  for (i=0;i<10;i++) 2 {3    int num = (int) ((Math.random ())); 4    5 }
View Code

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 0-100 integers, not includingthe. 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 TwoFinally, 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. about the difference between specifying a seed number and not specifying a seed number:http://blog.sina.com.cn/s/blog_93dc666c0101h3gd.html

The random () function in Java

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.