[Java Basics] Three kinds of random number generation methods

[Java Basics] Three kinds of random number generation methods

 

table of Contents

                  Foreword:

The first type: new Random();

The second type: Math.random();

The third type: currentTimeMillis();


Foreword:

        Generating random numbers within a specified range is one of the most commonly used techniques. Programmers hope to process numerous business logics by means of random numbers, and during the testing process, they also hope to generate test cases containing a large number of numbers by means of random numbers.

        There are three ways to generate random numbers in Java:

     

The first type: new Random();

        The first one requires the help of the java.util.Random class to generate a random number generator, which is also the most commonly used one. There are two constructors, Random() and Random(long seed). The first is to use the current time as the default seed, and the second is to proceed with the specified seed value. After generation, different types of numbers are generated with the help of different sentences. The seed is the first value used to generate a random number. The mechanism is to transform the value of this seed into a certain point in the random number space through a function, and the generated random numbers are evenly distributed in the space. The random numbers generated later are all related to the previous random number.

        Taking Java as an example, we observe the nextInt(int) method of its Random object and find that this method will generate an integer with a random value between 0 and the parameter int. For example (assuming Random rand = newRandom();, the same below): rand.nextInt(100); This line of code will generate random numbers in the range [0,100). So what if you want to get random numbers in the interval [1~100]? Because the integers in the interval [0, 100) are actually integers in the interval [0, 99]. Therefore, the largest possible "integer" is 99. Since the value obtained by rand.nextInt(100) is in the interval [0, 99], add 1 to the left and right of this interval to get the interval [1, 100]. Therefore, the code is written as: rand.nextInt(100) + 1;

       The following is an example of obtaining a random number between 5-10:


import java.util.Random;

public class random {

public static void main(String[] args){

     Random rand = new Random();

     System.out.println(rand.nextInt(6) + 5);

}
}

       The same reason: the following is the method to obtain random two digits and random three digits:
 

rand.nextInt(90) + 10; //随机两位数
rand.nextInt(900) + 100;//随机三位数

       Summary: Want to get random numbers in a certain range:

       Random rand = new Random();

       int randomFigure=rand.nextInt(MAX-MIN + 1) + MIN; // randomFigure will be assigned a random number between MIN and MAX.

 

The second type: Math.random();

       The value returned by the second method is a double value of [0.0, 1.0). Because the double type number has a high precision, it can be regarded as a random number to a certain extent. With the help of (int) to perform type conversion, you can get an integer random number. Counted.

       The following is an example of obtaining a random number between 5-10:

public static void main(String[] args)
{    
    int randomFigure = (int) (5+Math.random()*6); 
    System.out.println(randomFigure);
}

       Summary: Want to get random numbers in a certain range: 

     (Data type) (MIN+Math.random()*(MAX-MIN+1)).

 

The third type: currentTimeMillis();

      As for the third method, although it is not commonly used, it is also a way of thinking. The method returns the number of long milliseconds from 0:00:00:00 on January 1, 1970 (this is related to the UNIX system) to the present. After taking the modulus, the random number within the required range can be obtained.

      The following is an example of obtaining a random number between 5-10: 

public class random {

public static void main(String[] args){    

long l = System.currentTimeMillis();

int randomFigure = (int)l%6+5;

System.out.println(randomFigure);
	}
}

       Summary: Want to get random numbers in a certain range:

       long l = System.currentTimeMillis();

       int randomFigure = (int) (l% (MAX-MIN) + MIN);

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/113486635