Math class and Random class

Source: Internet
Author: User
Tags square root

Math class

Java.lang.Math provides a series of static methods for scientific calculation, the parameters and return value types of the methods are generally double, if more powerful mathematical ability to calculate the relevant content in higher mathematics, you can use Apache Commons the following Math class library

Common methods of the math class:

ABS take absolute value
ACOs, ASIN, atan, cos, sin, tan trigonometric functions
sqrt square root
Pow (double A, double b), a^b
Max (double A, double b), take the maximum value
Min (double A, double b), take minimum value
Ceil (Double A), the smallest integer greater than a
Floor (double A), the largest integer less than a
Random (), returns the number 0.0 to 1.0
Long round (double a), type Double data A is converted to long (rounded)
Todegrees (double Angrad), radians converted to angle
Roradians (double angdeg), Angle converted to radians

/**************示例程序****************/public static void main(String[] args) {    // 取正相关操作    System.out.println(Math.ceil(3.1));    System.out.println(Math.floor(3.4));    System.out.println(Math.round(3.1));    System.out.println(Math.round(3.8));    System.out.println("##########################");        // 绝对值, 开方, a的b次幂相关操作    System.out.println(Math.abs(-1));    System.out.println(Math.abs(-1.1));    System.out.println(Math.sqrt(36));    System.out.println(Math.pow(2, 4));    System.out.println("##########################");        // Math类中常用的常量    System.out.println(Math.PI);    System.out.println(Math.E);    System.out.println("##########################");        // 随机数    System.out.println(Math.random());}/*4.03.034##########################11.16.016.0##########################3.1415926535897932.718281828459045##########################0.02732034556476759*/
Random class

In the

Math class, although there is a method for generating a random number math.random (), but the range of random numbers usually required is not the double type data between [0,1], you need to perform some complex operations on it. If you use the Math.random () The computation is too complex, you can use another way to get the random number, that is the random class, this class is specifically used to generate random numbers, and math.random () the bottom is called the Random Class Nextdouble () method

/****************** sample program *************************/import java.util.random;public static void Main (string[] args) {        Random rand = new Random ();    Randomly generates a double type of data between [0,1] System.out.println (rand.nextdouble ());        System.out.println ("#########################");    Generates integer data System.out.println (Rand.nextint ()) randomly within the allowable range of the int type;        System.out.println ("#########################");    Float type Data System.out.println (Rand.nextfloat ()) between randomly generated [0,1];        System.out.println ("#########################");    Randomly generates false or True System.out.println (Rand.nextboolean ());        System.out.println ("#########################");    Data System.out.println (Rand.nextint (10)) of type int between randomly generated [0,10];        System.out.println ("#########################");    Randomly generated data of type int between [20,30] System.out.println (Rand.nextint (10) + 20);        System.out.println ("#########################"); Randomly generated data of type int between [20,30] (this method is computationally complex) System.out.println ((int) (rand.nextdouble () * 10) + 20);   System.out.println ("#########################");} /*0.18579466820637747######################## #1695590674 ######################## #0.8908015#################### # # # # # # #false ######################## #9 ######################## #21 ######################## #25 #################### #####*/

Math class and Random class

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.