Although we can use the Math class to call its class method random() to return a random number between 0 and 1 (excluding 0 and 1), for example:
(int)(Math.random()*100)+1;//Get a random integer between 1 and 100 (including 1 and 100)
However, Java provides a more flexible Random class for obtaining random numbers, which is in the java.util package.
The constructor using the Random class is as follows:
publicRandom();publicRandom(longseed);
Create a Random object, in which the second constructor creates a Random object using the seed specified by the parameter seed. People habitually refer to the Random object as a random number generator . For example, the following random number generator random calls the nextInt() method without parameters to return a random integer:
Randomrandom=newRandom();random.nextInt();
If you want the random number generator random to return a random number between 0 and n (including 0 but excluding n), you can ask random to call the nextInt(int m) method with parameters (the parameter m must be a positive integer value) ,For example:
random.nextInt(100);
Returns a random integer between 0 and 100 (including 0 but not including 100).
If the program needs to randomly obtain two boolean values of true and false representing true and false, you can let random call the nextBoolean() method, for example:
random.nextBoolean();
Returns a random boolean value.