Random class (java.util)
The random algorithm implemented in the Random class is pseudo-random, that is, random with rules. When performing randomization, the number of origin of the random algorithm is called seeds, which performs a certain transformation based on the seeds, thereby generating the required random numbers.
Random objects with the same number of seeds, the random numbers generated by the same number of times are exactly the same. In other words, for two Random objects with the same seed number, the random numbers generated for the first time are exactly the same, and the random numbers generated for the second time are exactly the same. This requires special attention when generating multiple random numbers.
The following describes the use of the Random class, as well as how to generate a random array of specified intervals and the chances required in the implementation program.
1. Generation of Random objects
The Random class contains two constructors, which are introduced in sequence below:
a. public Random()
This construction method uses a number related to the relative time corresponding to the current system time as the seed number, and then uses this seed number to construct the Random object.
b. public Random(long seed)
This constructor can be created by formulating a seed number.
Sample code:
The code copy is as follows:
Random r = new Random();
Random r1 = new Random(10);
Again: the number of seeds is just the origin number of the random algorithm and has nothing to do with the interval of the generated random numbers.
2. Common methods in Random class
The methods in the Random class are relatively simple, and the functions of each method are also easy to understand. It should be noted that the random numbers generated by each method in the Random class are uniformly distributed, which means that the probability of numerical generation within the interval is equal. Here is a basic introduction to these methods:
a. public boolean nextBoolean()
The function of this method is to generate a random boolean value, and the probability of generating true and false values is equal, that is, both are 50%.
b. public double nextDouble()
The purpose of this method is to generate a random double value, with the value between [0, 1.0).
c, public int nextInt()
The purpose of this method is to generate a random int value, which is between -231 and 231-1.
If you need to generate an int value for a specified interval, you need to perform a certain mathematical transformation. For details, please refer to the code in the usage example below.
d, public int nextInt(int n)
The function of this method is to generate a random int value, which is within the interval of [0, n), that is, a random int value between 0 and n, containing 0 but not n.
If you want to generate an int value for a specified interval, you also need to perform a certain mathematical transformation. For details, please refer to the code in the usage example below.
e, public void setSeed(long seed)
The purpose of this method is to reset the number of seeds in the Random object. After setting the number of seeds, the Random object is the same as the Random object created with the new keyword.
3. Random class usage example
Using the Random class, it is generally to generate random numbers for a specified interval. The following will introduce how to generate random numbers for a corresponding interval one by one. The following codes that generate random numbers are generated using the following Random object r:
Random r = new Random();
a. Generate decimals of the interval [0, 1.0)
The code copy is as follows:
double d1 = r.nextDouble();
It is obtained directly using the nextDouble method.
b. Generate decimals of the interval [0,5.0)
The code copy is as follows:
double d2 = r.nextDouble() * 5;
Because the number interval generated by the nextDouble method is [0, 1.0), expanding the interval by 5 times is the required interval.
Similarly, to generate a random decimal in the interval [0,d) and d is any positive decimal, you only need to multiply the return value of the nextDouble method by d.
c. Generate decimals in the interval [1,2.5)
The code copy is as follows:
double d3 = r.nextDouble() * 1.5 + 1;
To generate a random decimal of the interval [1, 2.5) you only need to first generate a random number of the interval [0, 1.5) and then add 1 to the generated random number interval.
Similarly, to generate any random number in the range of decimal interval [d1, d2) that does not start from 0 (where d1 is not equal to 0), you only need to first generate a random number in the interval [0, d2-d1) and then add the generated random number interval to d1.
d. Generate any integer
The code copy is as follows:
int n1 = r.nextInt();
Just use the nextInt method directly.
e. Generate integers in interval [0,10)
The code copy is as follows:
int n2 = r.nextInt(10);
n2 = Math.abs(r.nextInt() % 10);
The above two lines of code can generate integers in the interval [0,10).
The first implementation is directly implemented using the nextInt(int n) method in the Random class.
In the second implementation, first call nextInt() method to generate an arbitrary int number. The interval generated by the number sum of 10 is (-10,10), and then find the absolute value of the interval, and the obtained interval is [0,10).
Similarly, to generate random integers in any interval [0, n) you can use the following code:
The code copy is as follows:
int n2 = r.nextInt(n);
n2 = Math.abs(r.nextInt() % n);
f. Generate integers in interval [0,10]
The code copy is as follows:
int n3 = r.nextInt(11);
n3 = Math.abs(r.nextInt() % 11);
Compared with the integer interval, the [0,10] interval and the [0,11) interval are equivalent, so an integer of the [0,11) interval is generated.
g. Generate integers in the interval [-3,15)
The code copy is as follows:
int n4 = r.nextInt(18) - 3;
n4 = Math.abs(r.nextInt() % 18) - 3;
To generate random integers that do not start from 0, you can refer to the above description of the implementation principle of the decimal interval that does not start from 0.
h, chance to achieve
Implementing program logic according to a certain chance is also a problem that can be solved by random processing. Here is a simple example to demonstrate how to implement the logic of chance using random numbers.
In the previous method introduction, the numbers generated in the nextInt(int n) method are uniform, that is, the chances of generation of each number within the interval are the same. Then if a random integer in the interval [0,100) is generated, the chances of each number being generated should be the same, and since there are 100 integers in the interval, the chances of each number are 1%. According to this theory, the probability problem in the program can be realized.
Example: Randomly generate an integer that generates 1 with 55% chance, 2 with 40% chance, and 3 with 5% chance. The implemented code is as follows:
The code copy is as follows:
int n5 = r.nextInt(100);
int m; //Result number
if(n5 < 55){ //The interval of 55 numbers, 55% chance
m = 1;
}else if(n5 < 95){//[55,95), interval of 40 numbers, 40% chance
m = 2;
}else{
m = 3;
}
Because the probability of each number is 1%, the probability of any 55 digit interval is 55%. For the convenience of writing the code, all integers in the [0,55) interval are used here, and the subsequent principle is the same.
Of course, the code here can be simplified because the odds are multiples of 5%, so just control the chances based on 5%. The following is the simplified code implementation:
The code copy is as follows:
int n6 = r.nextInt(20);
int m1;
if(n6 < 11){
m1 = 1;
}else if(n6 < 19){
m1 = 2;
}else{
m1 = 3;
}
Inside the program, the logic of probability can be implemented according to the above instructions.
4. Other issues
a. The problem of Random object with the same seed number
As mentioned earlier, Random objects with the same number of seeds have the same random numbers generated by the same number of times. The following is the test code:
The code copy is 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 are 10, so the random numbers generated by the same number of times are exactly the same.
If you want to avoid the situation where random numbers are the same, you need to note that no matter how many random numbers you need to generate in the project, you can only use one Random object.
b. About the random method in the Math class
In fact, there is also a random method in the Math class. The job of this random method is to generate a random decimal of the interval [0, 1.0).
By reading the source code of the Math class, we can find that the random method in the Math class is implemented by directly calling the nextDouble method in the Random class.
It's just that the random method is called relatively simple, so many programmers are accustomed to using the random method of the Math class to generate random numbers.