Java Random Number
. java math.random () method to produce a random number, the resulting random number is 0-1 double We can multiply him by a certain number, for example, by multiplying the 100 He's a . 100
Two . in the Java.util This package provides a Random class, we can create a new Random object to produce a random number, he can produce random integers, random float , Random Double , Random Long , this is where we Java a method used in a program to take random numbers.
three.system Span style= "font-family: ' The song Body '; The class has a currenttimemillis () method, this method returns a 1970 year 1 month 1 0 dot 0 Sub 0 seconds to the current number of milliseconds, the return type is long We can take him as a random number, and we can take him to a number of models, you can limit him to a range of .
Various explanations:
Java.lang.Math.Random;
Call this math.random () function to return a double with a positive value 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 the range (approximate) evenly distributed.
int num = (int) (Math.random ())
above Code in the value of NUM will be uniform randomly distributed between [0,3] ! When using math.random () , the function is to return a value of type double , so when assigning to a variable of another type it is necessary to pay attention to type conversions.
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, there are regular random, so-called rule is in the given species (seed) Randomly generated numbers within the interval;
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;
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 derived from this random number generator sequence, evenly distributed between 0.0 and 1.0 . A double value.
5.float nextfloat (): Returns the next pseudo-random number, which is derived from this random number generator sequence, evenly distributed between 0.0 and 1.0 float value.
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 The standard deviation is 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 generatorwith a single long seed.
Summarize:
Finally, let's compare the characteristics of these two random functions in a nutshell:
1.java. Math.random () is actually called inside of Java.util.Random () , It has a fatal weakness, it is related to the system time, In other words, the two random times are short, 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.
Java Record -38-random number