Basic part of Java SE -- Math and Random classes of common class libraries generate Random values, mathrandom

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.