Basic part of Java SE -- Math and Random classes of common class libraries generate Random values, mathrandom
1 // 20160518 Math class common method Exercise 2 package MyPackage; 3 4 public class MathDemo {// define the main class and main method 5 6 public static void main (String [] args) {7 System. out. println (Math. sqrt (16); // Math class common method: Calculate the square root of 8 System. out. println (Math. pow (2, 3); // Math class common method: the number of power 9 System. out. println (Math. round (13.64); // Math class common methods: Rounding 10 systems. out. println (Math. random (); // Math class common method: generate a random number 11} 12 13}
The methods in the Math class are static methods. You can call them directly in the form of "class. Method Name.
Bytes ---------------------------------------------------------------------------------------------
1 // 20150518 Random class: randomly generate a number to Exercise 2 package MyPackage; // custom package 3 4 import java. util. random; // import the required Random Package 5 6 public class RandomDemo {// define the main class 7 public static void main (String [] args) {// mian Method 8 Random r = new Random (); // instantiate the Random Class Object r 9 for (int I = 0; I <6; I ++) {// Input 6 random numbers for 6 cycles: 10 System. out. print (r. nextInt (100) + "\ t"); // generate a random number and specify a value range less than 10011} 12} 13 14}
The Random class randomly generates values and specifies the range of values to be generated.