Introduction Method 1 2 public int nextInt (int maxValue) generated [0, a random integer maxValue) range, including 0, that does not contain maxValue; . 3 public Double nextDouble () generates a random decimal range [0,1), comprising 0.0, not It contains 1.0.
1 Random use: 2 Import leader packet: packet belongs java.util.Random . 3 format to create an instance: Random variable name = new Random ();
/ * Java reference has been some type of Random class, function, random number generating steps: 1. Import package, Random class, also java.util folder 2. formula: create Random types of variables . 3. Random Variables call class functions, random number generation random class provides functionality, name nextInt () generates a random number, the result is an int range appear random number function nextInt (write a integer) integer: range random out of a random number the range of 0 - random nextInt (100) between the specified integer [0,99) generating random floating point numbers: function name nextDouble () random number range [0.0,1.0) random number: pseudo-random number, the virtual machine a person written algorithm generates out * / Import java.util.Random; public class RandomDemo { public static void main (String [] args) { // 2. formula: create a variable of type Random Random = RAN new newRandom (); // . 3. Random variable class function calling, generates a random number // Random class, random number generation function int I = ran.nextInt ( 100 ); the System. OUT .println (I) ; // ? question generates a random number, the range 1-100 // the nextInt (100) + 0-99. 1 Double D = ran.nextDouble (); the System. OUT .println (D); } }
Here highlight some of generating random numbers specified range:
int ranNumber = ran.nextInt (max - min + 1) + min; // ranNumber would be assigned to a random number in a range of min and max [min, max]
// Java generates a random number within the specified range // int ranNumber ran.nextInt = (max - min +. 1) + min; // ranNumber would be assigned to a random number in a range of min and max [min, max] // example: generating a random number between [155,658] int ranNumber ran.nextInt = ( 504 ) + 155 ; the System. OUT .println (ranNumber);