Random ()
Create a new random number generator.
The code is as follows: |
Copy code |
Random (long seed) |
Use a single long seed to create a new random number generator.
We can specify the seed when constructing a Random object, for example:
The code is as follows: |
Copy code |
Random r1 = new Random (20 ); |
Or the number of seeds corresponding to the relative time corresponding to the current system time is used by default:
The code is as follows: |
Copy code |
Random r1 = new Random (); |
The seed number 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
Below are some basic introductions to these methods:
The code is as follows: |
Copy code |
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%.
The code is as follows: |
Copy code |
B. public double nextDouble () |
This method generates a random double value between 0 and 1.0.
The code is as follows: |
Copy code |
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.
The code is as follows: |
Copy code |
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.
The code is as follows: |
Copy code |
E. public void setSeed (long seed) |
This method is used to reset the number of seeds in the Random object. After setting the number of seeds, use the new keyword to create the Random object and 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
The code uses the following Random object r to generate:
The code is as follows: |
Copy code |
Random r = new Random (); |
A. Generate decimals in the range [0, 1.0)
The code is as follows: |
Copy code |
Double d1 = r. nextDouble (); |
Directly use the nextDouble method.
B. Generate decimals in the range [0, 5.0)
The code is as follows: |
Copy code |
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)
The code is as follows: |
Copy code |
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 the range of [0, d2-d1)
Random number, and then add the generated random number range to d1.
D. Generate any integer
The code is as follows: |
Copy code |
Int n1 = r. nextInt (); |
Use the nextInt method directly.
E. Generate an integer in the range [).
The code is as follows: |
Copy code |
Int n2 = r. nextInt (10 ); N2 = Math. abs (r. nextInt () % 10 ); |
The above two lines of code can generate an integer in the range [)
Uniform distribution
The code is as follows: |
Copy code |
Import java. util. Random; Public class RandomDemo { /** * Guy tribe * * @ Param args */ Public static void main (String [] args ){ Random r = new Random (); Int n5 = r. nextInt (100 ); String ran = ""; If (n5 <55) {// The interval of 55 digits, the probability of 55% Ran = "55% "; } Else if (n5 <95) {// [40%), 40 digit range, probability Ran = "40% "; } Else { Ran = "5%"; // you can understand the residual probability! } System. out. println (ran ); } } |