The random class, which is used to generate stochastic numbers for a specified interval, has a constructor with no parameters and two parameters, with parameters of the constructor whose parameters are the number of seeds.
Assume that the following r is a random object
Random r = new Random ();
1. Generate any integer
int i = R.nextint ();
2. Generate integers for [0,n] intervals (two kinds)
int i= r.nextint (n+1);
N3 = Math.Abs (R.nextint ()% (n+1));
3. generating integers for [0,n] intervals
int i= r.nextint (n);
4. Generate decimals for [0,1.0] Interval
Double d = r.nextdouble ();
5. Generate decimals for [1,2.5] Interval
Double d = r.nextdouble () * 1.5 + 1;
It is worth noting that if the constructor of R is carrying parameters
Random r = new Random (n)
The parameter n is a long type, called the seed number, and the random object that has the same number of seeds, the same number of occurrences is exactly the same.
Tests such as
Therefore, in the actual project, only one can be used.
In addition, math provides a function random () to generate a pseudo-random double value that is greater than or equal to 0.0 and less than 1.0.
Use it--the probability of using and generating random numbers