The usage of JAVA class Random

  • 2020-05-09 18:40:01
  • OfStack

Random class (java.util)                

The random algorithm implemented in Random class is pseudorandom, that is, random with rules. In the process of randomization, the origin number of the random algorithm is called the seed number (seed), and a definite transformation is carried out on the basis of the seed number to generate the required random number.

For 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 number of seeds, the random Numbers generated the first time are exactly the same, and the random Numbers generated the second time are exactly the same. This is especially important when generating multiple random Numbers.

The following section describes the use of the Random class, how to generate a random number group for a specified interval, and how to implement the probability required in the program.

1. Generation of Random objects

The Random class contains two constructors, which are described in turn:

a, public Random ()

The constructor USES a number related to the relative time of the current system time as the seed number, which is then used to construct the Random object.

b, public Random(long seed)

The constructor can be created by specifying the number of seeds.

Sample code:


Random r = new Random();
Random r1 = new Random(10);

Again, the seed number is only the origin number of the random algorithm, and has nothing to do with the interval of the generated random number.

2. Common methods in the Random class

The methods in the Random class are simple, and the functionality of each method is easy to understand. It should be noted that the random Numbers generated by the methods in Random class are uniformly distributed, which means that the Numbers generated within the interval are equally likely to be generated. The following is a basic introduction to these methods:

a, public boolean nextBoolean()

The effect of this method is to generate a random boolean value, and the probability of generating true and false values is equal, that is, both have a 50% chance.

b, public double nextDouble()

The purpose of this method is to generate a random double value between [0,1.0].

c, public int nextInt()

The function of this method is to generate a random int value between the range of int, i.e., between -231 and 231-1.

If you need to generate int values for a specified interval, you need to perform a definite mathematical transformation, as shown in the code in the use example below.

d, public int nextInt(int n)

The purpose of this method is to generate a random int value between [0,n], which is a random int value between 0 and n, containing 0 but not n.

If you want to generate the int value of the specified interval, you also need to perform a definite mathematical transformation. For details, see the code in the use example below.

e, public void setSeed(long seed)

This method resets the number of seeds in the Random object. The Random object after the seed number is set is the same as the Random object created with the same seed number using the new keyword.

3. Example of Random class

Using the Random class, 1 is generally used to generate random Numbers for the specified interval. The following 11 shows how to generate random Numbers for the corresponding interval. The following codes to generate random Numbers are generated using the following Random object r:

Random r = new Random();

a, generate a decimal in the [0,1.0) interval


double d1 = r.nextDouble();

Obtained directly using the nextDouble method.

b, generates a decimal in the interval [0,5.0]


double d2 = r.nextDouble() * 5;

Since 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 an arbitrary positive decimal in d, you just need to multiply the return value of the nextDouble method by d.

c, generating decimals in the [1,2.5] interval


double d3 = r.nextDouble() * 1.5 + 1;

To generate a random decimal in the interval [1,2.5], you only need to first generate a random number in the interval [0,1.5], and then add 1 to the interval of the generated random number.

Similarly, to generate any random digit in the range of [d1,d2] that does not start from 0 (where d1 does not equal 0), you only need to first generate the random digit in the range of [0,d2-d1), and then add d1 to the generated random digit.

d, generate any integer


int n1 = r.nextInt();

Just use the nextInt method.

e, generates an integer in the interval [0,10)


int n2 = r.nextInt(10);
n2 = Math.abs(r.nextInt() % 10);

Both lines of code can generate integers in the interval [0,10].

The first implementation is implemented directly using the nextInt(int n) method in the Random class.

In the second implementation, the nextInt() method is first called to generate an arbitrary int number, whose interval with 10 is (-10,10), and then the absolute value of the interval is taken, and the interval is [0,10].

Similarly, the following code can be used to generate random integers in any [0,n) interval:


int n2 = r.nextInt(n);
n2 = Math.abs(r.nextInt() % n);

f, generates the integers in the interval [0,10]


int n3 = r.nextInt(11);
n3 = Math.abs(r.nextInt() % 11);

The [0,10] interval is equivalent to the [0,11] interval relative to the integer interval, so the integer of the [0,11] interval is generated.

g, generates an integer in the interval [-3,15]


int n4 = r.nextInt(18) - 3;
n4 = Math.abs(r.nextInt() % 18) - 3;

To generate random integers that do not start at 0, see the non-0 decimal interval implementation principle above.

h, probability implementation

Implementing program logic with a fixed probability of 1 is also a problem that can be solved by random processing. Here is a simple example of how to implement the probability logic using random Numbers.

In the previous method introduction, the Numbers generated in the nextInt(int n) method are uniform, which means that the probability of each number being generated inside the range is the same. So if you generate a random integer in an interval of [0,100], each number should have the same probability, and since there are a total of 100 integers in that interval, each number has a 1% chance. According to this theory, the probability problem in the program can be implemented.

Example: randomly generate an integer that generates a 1 55% of the time, a 2 40% of the time, and a 3 5% of the time. The implementation code is as follows:


int n5 = r.nextInt(100);
int m; // The results of digital
if(n5 < 55){ //55 The interval between the Numbers, 55% The risk of
    m = 1;
}else if(n5 < 95){//[55,95) . 40 The interval between the Numbers, 40% The risk of
    m = 2;
}else{
    m = 3;
}

Since the probability of each number is 1%, the probability of any interval of 55 digits is 55%. For the convenience of writing the code, all integers in the interval of [0,55] are used here.

Of course, the code here can be simplified, because the probability is a multiple of 5%, so as long as the probability is controlled based on 5%, the following is the simplified code implementation:


double d1 = r.nextDouble();
0

Within the program, the probability logic can be implemented as described above.

4. Other questions

a, same number of seeds Random object problem

As mentioned above, the Random object with the same number of seeds will generate the same random number for the same number of times. The following is the code of the test:


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, objects r1 and r2 both use a seed number of 10, so the random Numbers generated by the two objects the same number of times are exactly the same.

If you want to avoid situations where random Numbers are the same, it is important to note that no matter how many random Numbers you need to generate in your project, only one Random object is used.

b, about the random method in the Math class

In fact, there is also an random method in the Math class, whose job is to generate a random decimal between [0,1.0].

By reading the source code of Math class, you can find that the random method in Math class is directly implemented by calling the nextDouble method in Random class.

It's just that the random method is easy to call, so many programmers are used to using the random method of the Math class to generate random Numbers.


Related articles: