Java-Common functions

Source: Internet
Author: User

http://blog.csdn.net/pipisorry/article/details/44411541

random () function generates stochastic number Java.util.RandomIn the Java API Help document, a summary of the random () function of the description: 1, the Java.util.Random class implementation of the random algorithm is pseudo-random, that is, random rules, so-called rule is a given species (seed) in the interval randomly generated numbers 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 intra-interval digital generation is equal; two ways to construct Random () 1.Random ():creates a new random number generator. 2.Random (longseed):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 does, and then look down), such as: random r1 = new Random (20), or the default current system time corresponds to the relative time of the number as the seed number: random r1 = new Random (); What needs to be explained is 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. such as the following Java code: Random rand =new random (+), int i;i=rand.nextint (100); initialization 25 does not have a direct effect (note: It does not work), rand.nextint (100); The 100 is the upper limit of the random number, resulting in a random number of 0-100 integers, not including 100.
The Random () method 1.protectedint Next (intbits): 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 (INTN): Returns a pseudo-random number, which is an int value that is derived from the sequence of this random number generator, 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 (longseed): Sets the seed of this random number generator with a single long seed.

Example:
1. Generate a decimal number for the [0,1.0] interval: double D1 =r.nextdouble (), 2. Decimal for the generation of the [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. Generates an integer between 231 and 231-1: int n =r.nextint (); 5. Integer that generates the [0,10] interval: int n2 = R.nextint (10);//Method One N2 = Math.Abs (R.nextint ()%10);//Method Two
The function of the random number seed:
Examples 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.  




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).


The characteristics of the 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, 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. from:http://blog.csdn.net/pipisorry/article/details/44411541

Ref:http://blog.sina.com.cn/s/blog_93dc666c0101h3gd.html



Java-Common functions

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.