Random Class (Java.util)
The stochastic algorithm implemented in the random class is pseudo random, that is, random with rules. At random, the origin number of the random algorithm is called seed number, and a certain transformation is made on the basis of the seed number, which produces the random number needed.
The random object of the same seed number, the same number of random numbers generated are exactly the same. That is, two random objects with the same seed count, the first generation of random numbers are exactly the same, and the second generation of random numbers is exactly the same. This requires special attention when generating multiple random numbers.
The following describes the use of the random class and how to generate a random array of specified intervals and the probability of implementing the requirements in the program.
1, the generation of random objects
The random class contains two construction methods, which are described in turn:
A, public Random ()
The constructor uses a number that corresponds to the relative time of the current system time as the number of seeds, and then constructs the random object using this seed number.
B, public Random (long Seed)
The construction method can be created by making a number of seeds.
Sample code:
Copy Code code as follows:
Random r = new Random ();
Random r1 = new Random (10);
Again: The seed number is only the origin number of the random algorithm, regardless of the interval of the generated random number.
2. Common methods in random class
The methods in the random class are simple, and the functionality of each method is easy to understand. It should be explained that the random numbers generated by each method in the random class are evenly distributed, which means that the probability of the generation of the numbers within the interval is equal. Here's a basic introduction to these methods:
A, public boolean nextboolean ()
The effect of this method is to generate a random Boolean value that yields true and false values equal to the probability of 50%.
B, public double nextdouble ()
The effect of this method is to generate a random double value between [0,1.0].
c, public int nextint ()
The effect of this method is to generate a random int value that is between the range of int, that is, between 231 and 231-1.
If you need to generate an int value for a specified interval, you need to make some mathematical transformations, depending on the code in the following example.
d, public int nextint (int n)
The effect of this method is to generate a random int value that is in the range of [0,n], that is, a random int value between 0 and N, containing 0 without n.
If you want to generate an int value for a specified interval, you also need to make some mathematical transformations, depending on the code in the following example.
E, public void setseed (long Seed)
The purpose of this method is to reset the number of seeds in the random object. After the seed count is set, the random object and the same seed number are created with the same random object as the New keyword.
3, Random class use example
Using the random class, which typically generates random numbers for a specified interval, the following describes how to generate random numbers of corresponding intervals. The following code for generating random numbers is generated using the following random object R:
Random r = new Random ();
A, the decimal number of the generation [0,1.0] Interval
Copy Code code as follows:
Double D1 = r.nextdouble ();
Directly using the Nextdouble method to obtain.
b, the decimal number of the generation [0,5.0] Interval
Copy Code code as follows:
Double D2 = r.nextdouble () * 5;
Because the Nextdouble method generates a numerical interval of [0,1.0], extending the interval by 5 times times is the required interval.
Similarly, to generate a random decimal number in a [0,d) interval, d is any positive decimal, you only need to multiply the return value of the Nextdouble method by D.
C, Generation [1,2.5] Interval decimal
Copy Code code as follows:
Double d3 = r.nextdouble () * 1.5 + 1;
To generate a random decimal number in the [1,2.5] interval, you only need to generate the random numbers of the [0,1.5] interval first, and then add a random number interval of 1.
Similarly, to generate random numbers (where D1 is not equal to 0) of any range of decimal intervals [D1,D2] that do not start from 0, you only need to generate a random number of [0,d2-d1] intervals, and then add D1 to the generated random number interval.
d, generate any integer
Copy Code code as follows:
Use the Nextint method directly.
E, integers that generate [0,10] intervals
Copy Code code as follows:
int n2 = R.nextint (10);
N2 = Math.Abs (r.nextint ()% 10);
Both lines of code can generate integers in the [0,10] interval.
The first implementation is implemented directly using the nextint (int n) method in the Random class.
In the second implementation, first call the Nextint () method to generate an arbitrary int number, the number and 10 after the generation of the numerical interval is ( -10,10), and then the interval for the absolute value, the resulting interval is [0,10).
Similarly, random integers that generate arbitrary [0,n] intervals can use the following code:
Copy Code code as follows:
int n2 = R.nextint (n);
N2 = Math.Abs (r.nextint ()% n);
F, integers that generate [0,10] intervals
Copy Code code as follows:
int n3 = R.nextint (11);
N3 = Math.Abs (R.nextint ()% 11);
Relative to the integer interval, [0,10] intervals and [0,11] intervals are equivalent, so the integers of the [0,11) interval are generated.
g, integers that generate [ -3,15] intervals
Copy Code code as follows:
int N4 = R.nextint (18)-3;
N4 = Math.Abs (R.nextint ()% 18)-3;
To generate a random integer that is not from the 0 start interval, you can see a description of the implementation principle of the decimal interval, which is not starting from 0.
H, Probability realization
The realization of program logic according to certain probability is also a problem that can be solved by random processing. The following is a simple example that shows how to use the logic of random numbers to implement probabilities.
In the previous method introduction, the number generated in the nextint (int n) method is homogeneous, meaning that the probability of each number generated within the interval is the same. If you generate a random integer for a [0,100] interval, the probability of each number generation is the same, and because there are 100 integers in the range, the probability of each number is 1%. According to this theory, the probability problem in the program can be realized.
Example: Randomly generating an integer that generates 1 at a 55% probability, generates 2 at a 40% probability, and generates 5% at 3. The implementation code is as follows:
Copy Code code as follows:
int n5 = r.nextint (100);
int m; Result number
if (N5 < 55) {//55 digit interval, 55% probability
m = 1;
}else if (N5 < 95) {//[55,95), 40-digit interval, 40% probability
m = 2;
}else{
m = 3;
}
Because the probability of each number is 1%, the probability of an arbitrary 55-digit interval is 55%, and for code to write, it uses all the integers in the [0,55) interval, followed by the principle.
Of course, the code here can be simplified because the probabilities are multiples of 5%, so as long as the 5% is the basis to control the probability, the following is a simplified code implementation:
Copy Code code as follows:
int N6 = R.nextint (20);
int M1;
if (N6 < 11) {
m1 = 1;
}else if (N6 < 19) {
M1 = 2;
}else{
m1 = 3;
}
Within the program, the logic of probability can be implemented in accordance with the above instructions.
4, other issues
A, the same seed number random object problem
As described earlier, the random object with the same number of seeds, the random numbers generated at the same times are exactly the same, and the following is the code for the test:
Copy Code code as follows:
Random r1 = new Random (10);
Random r2 = new Random (10);
for (int i = 0;i < 2;i++) {
System.out.println (R1.nextint ());
System.out.println (R2.nextint ());
}
In this code, the number of seeds used by objects R1 and R2 is 10, then the random numbers generated by the same number of objects are exactly the same.
If you want to avoid situations where random numbers are the same, be aware that no matter how many random numbers you need to generate in your project, you can use only one random object.
b, about the random method in the math class
In fact, there is also a random method in the math class, which works by generating random decimals for a [0,1.0] interval.
By reading the source code of the math class, you can find that the random method in the math class is directly called the Nextdouble method in the random class.
Just random method calls are simpler, so many programmers are accustomed to using the random method of the math class to generate random numbers.