For this problem, I only put the code on it, the code has comments, believe that smart you can understand!
ImportJava.util.Random;//need to use this class package/** Java generates a random number * time:2017/10/16 * method One: The random method in the Math class can be used to generate the number of randomly generated * the Math.random () method returns a double with a positive sign, which is greater than 0 less than 1. * Of course, we can also make changes: * (int) (Math.random () *10) returns the random number between 0-9 * (int) (math.random () *n) returns a random number between 0-n * (int) (Math.random () *100) +1 returns a random number between 0-100, preceded by 0-99, plus 1 is 0-100 * * Method Two: The Random class * must be written before generating an arbitrary number: random random=new random (); * Random.nextint (); Returns a random number in the range of type int * Random.nextint (10); Returns a random number between 0-9 * random.nextint (100) +1; Returns a random number between 1-100 * Random.nextint (n) +m; The return is the random number between M and m+n-1.*/ Public classRandomnum { Public Static voidMain (string[] args) {//Method One DoubleNum=math.random ();//returns a number of type doubleSystem.out.println (num); //The following generates an int type if the method is not enclosed, only 0 is returned intNum1= (int) (Math.random () *10);//returns the random number between 0-9System.out.println (NUM1); intNum2= (int) (Math.random () *100);//returns the expedited between 0-99System.out.println (num2); //Method TwoRandom random=NewRandom ();//method Two must write this sentence, random is just a name, take intS=random.nextint ();//generates a random number of type intSystem.out.println ("Generated random number is:" +s); intS2=random.nextint (10);//generate a random number between 0-9System.out.println (S2); }}
View Code
The result of the operation is:
the random number generated by 0.3248983535648986691 is:-19098713886
Java generates random numbers