java generate random numbers in a specific range

Generating random numbers within a specified range is one of the most commonly used techniques. Programmers hope to use random numbers to process many business logics, and also hope to generate test cases containing a large number of numbers by means of random numbers during the testing process. The problem is often similar to:
how to randomly generate a random number between 1 and 100, and the value includes the boundary values ​​1 and 100. Or: How do I randomly generate a random 3-digit integer? Wait...
Taking the Java language 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. For example (assuming Random rand = new Random();, the same below):
rand.nextInt(100);
This line of code will generate random numbers in the range 0~100. Interestingly, the value may be 0. But it can't be 100. We use the interval notation learned in middle school math class, expressed as: [0, 100). So if you want to get a random number in the interval [1~100], what should you do? With a little brainstorming, you can think of: the integers in the interval [0, 100) are actually the interval [0, 99]. Because the maximum bound is 100, unfortunately it cannot be equal to 100, so the maximum possible "integer" is 99. Since the value obtained by rand.nextInt(100) is the interval [0, 99], then 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
; Running the code below will get 10 values ​​of [1, 100].
import java.util.Random;
public class Test {
	public static void main(String[] args){
	  Random rand = new Random();
	  for(int i=0; i<10; i++) {
	   System.out.println(rand.nextInt(100) + 1);
	  }
	}
}
Compile and run, the output result is:
81
64
31
86
56
14
45
57
28
90
times, the result is different each time, but the value must be between 1 and 100, and 1 and 100 may appear. In the same way, it is easy to know that if you want to get a random two-digit integer, the code is written as:
rand.nextInt(90) + 10;
You must be surprised why it is written like this. In fact, the number 90 as a parameter in the nextInt() method means: the number of possibilities of all the values ​​of the random number you want to generate (in this proposition, the two-digit integer value is [10, 99], a total of 90 number); add the following number 10 to indicate the minimum value of the interval.
You can verify that, according to this understanding, the random number of [1, 100] should be written as rand.nextInt(100) + 1. Do not interpret the parameter 100 as the maximum value. It's just that the interval [1, 100] starts at exactly 1, so the maximum value and the number of possible values ​​are exactly 100. So
the code to generate a random three-digit number is:
rand.nextInt(900) + 100; the code to
generate a random value in the interval [64, 128] is:
rand.nextInt(65) + 64;
the number of possible values ​​is How is it calculated? Of course, the maximum value - the minimum value + 1, so the final formula is as follows:
int randNumber = rand.nextInt(MAX - MIN + 1) + MIN; // randNumber will be assigned a random number between MIN and MAX

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325886507&siteId=291194637