Use -- random number usage and probability of generation, random number Probability
The Random class is used to generate Random numbers within a specified range. The constructor has two types of constructor without parameters and parameters. The constructor with parameters has 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 an integer in the range [0, n] (two types)
Int I = r. nextInt (n + 1 );
N3 = Math. abs (r. nextInt () % (n + 1 ));
3. generate an integer in the [0, n) Interval
Int I = r. nextInt (n );
4. Generate decimal places in the range [0, 1.0)
Double d = r. nextDouble ();
5. Generate decimal places in the range [1, 2.5)
Double d = r. nextDouble () * 1.5 + 1;
It is worth noting that if the r constructor carries Parameters
Random r = new Random (n)
The n parameter is of the long type. It is called the number of seeds. a Random number is generated with the same number of seeds.
Test as shown in Figure
Therefore, you can use only one project.
In addition, Math also provides a function random () to generate a pseudo-random double value greater than or equal to 0.0 and less than 1.0.