1. Generate a random object
The random class contains two constructor methods, which are described in sequence below:
A. Public random ()
This constructor uses a number related to the relative time of the current system time as the number of seeds, and then uses this number to construct the random object.
B. Public random (long seed)
This constructor can be created by creating a number of seeds.
Sample Code:
Random r = new random ();
Random R1 = new random (10 );
Again, the number of seeds is only the source number of the random algorithm, and is irrelevant to the interval of the generated random number.
2. Common methods in the random class
Methods In the random class are relatively simple, and the functions of each method are easy to understand. It should be noted that the random numbers generated by each method in the random class are evenly distributed, that is, the number generation probability within the range is equal. Below are some basic introductions to these methods:
A, public Boolean nextboolean ()
This method is used to generate a random Boolean value. The probability of true and false values generated is equal, that is, the probability is 50%.
B. Public double nextdouble ()
This method generates a random double value between 0 and 1.0.
C, public int nextint ()
The function of this method is to generate a random int value, which is between the int range, that is, between-231 and 231-1.
If you need to generate the int value of the specified range, you need to perform a certain mathematical transformation. For details, see the Code in the following example.
D. Public int nextint (int n)
The function of this method is to generate a random int value, which is an interval between [0, n), that is, a random int value between 0 and N, contains 0, not n.
If you want to generate the int value of the specified range, you also need to perform a certain mathematical transformation. For details, see the Code in the following example.
E. Public void setseed (long seed)
This method is used to reset the number of seeds in the random object. The random object after the number of seeds is set is the same as the random object created with the new keyword for the same number of seeds.
3. Random usage example
The random class is generally used to generate random numbers for the specified range. The following describes how to generate random numbers for the corresponding range. The following Random Number Generation Code uses the following random object R:
Random r = new random ();
A. Generate Decimals in the range [0, 1.0)
Double d1 = R. nextdouble ();
Directly use the nextdouble method.
B. Generate Decimals in the range [0, 5.0)
Double D2 = R. nextdouble () * 5;
Because the number range generated by the nextdouble method is [0, 1.0), it is required to increase the range by 5 times.
Similarly, generate a random decimal number in the [0, d) interval. If D is an arbitrary positive decimal number, multiply the return value of nextdouble by D.
C. Generate Decimals in the range [1, 2.5)
Double D3 = R. nextdouble () * 1.5 + 1;
To generate a random decimal number in the range [1, 2.5), you only need to first generate a random number in the range [0, 1.5), and then add 1 to the random number range.
Similarly, to generate random numbers in the range of any decimal range [D1, D2) not starting from 0 (where D1 is not equal to 0), you only need to first generate [0, d2-d1) then add the generated random number range to the D1.
D. generate any integer
Int n1 = R. nextint ();
Use the nextint method directly.
E. generate an integer in the range [).
Int n2 = R. nextint (10 );
N2 = math. Abs (R. nextint () % 10 );
The above two lines of code can generate an integer in the [0, 10) range.
The first method is directly implemented using the nextint (int n) method in the random class.
In the second implementation, call the nextint () method to generate an arbitrary int number. The value ranges from-10 to 10 ), then calculate the absolute value of the range, and the obtained range is.
Similarly, the following code can be used to generate random integers in any [0, n) range:
Int n2 = R. nextint (N );
N2 = math. Abs (R. nextint () % N );
F. generate an integer in the range [0, 10 ].
Int N3 = R. nextint (11 );
N3 = math. Abs (R. nextint () % 11 );
The range [] is equivalent to the range [). Therefore, an integer from [) is generated.
G. generate an integer in the range [-).
Int N4 = R. nextint (18)-3;
N4 = math. Abs (R. nextint () % 18)-3;
Generate a random integer that is not in the range starting from 0. For details, see the implementation principle of the fractional interval that is not in the range starting from 0.
Math. the random () method generates a double-type random number between 0.0 and 1.0 (excluding 1.0). Generally, A + math. random () * B can return a random number between A and A + B, excluding a + B.