Summary of common functions of math class in Java

Source: Internet
Author: User
Tags natural logarithm root math square root

A summary of some of the more common mathematical formulas in Java:

Rounding, returns the largest integer less than the target function, which will return -2math.floor (-1.8);//rounding, returning the smallest integer math.ceil ()/Rounded rounding Math.Round ()// Computes the square root math.sqrt ()//calculates the cubic root math.cbrt ()//returns the N-Power Math.exp (3) of Euler's number E;  Calculate the powers, the following is calculated 3 of the 2 square Math.pow (3,2);  Calculates the natural logarithm Math.log (); //Calculate absolute value math.abs (); //Calculate maximum value Math.max (2.3,4.5);  Calculates the minimum value of math.min (,); //Returns a pseudo-random number that is greater than or equal to 0.0 and less than 1.0math.random              

The random class is specifically used to generate a pseudo-random number, which has two constructors: one constructor uses the default seed (the current time as the seed), and the other constructor requires the programmer to display the seed of a long integer passed in.

The random () method from math provides more ways to generate various pseudo-random numbers.

e.g

Import Java.util.Arrays;Import Java.util.Random;PublicClassrandomtest {/** *@param args */PublicStaticvoid Main (string[] args) { TODO auto-generated Method StubRandom Rand =New Random ();System.out.println ("Random boolean number" + Rand.nextboolean ()); byte[] Buffer =Newbyte[16];Rand.nextbytes (buffer); Produces an array of random numbers containing 16 elements of an arraySYSTEM.OUT.PRINTLN (arrays.tostring (buffer));  System.out.println ( "rand.nextdouble ()" + Rand.nextdouble ());   System.out.println (  System.out.println ( "Rand.nextgaussian" + Rand.nextgaussian ());   System.out.println (  //production of a 0~32 random integer  System.out.println ( "Rand.nextint (+)" + rand.nextint (32);   System.out.println ( "Rand.nextlong" + Rand.nextlong ());}}    

To prevent two random objects from producing the same sequence of numbers, it is generally recommended to use the current time as the seed of the Random object, as follows:

Random rand = new Random (System.currenttimemillis ());

Threadlocalrandom was introduced into the JAVA7.

In the case of multithreading, the use of threadlocalrandom is similar to the use of random, the following program/fragment demonstrates the use of Threadlocalrandom:

Use Nextcxxx () to generate the desired pseudo-random sequence after using current () to generate a random sequence:

Threadlocalrandom trand= threadlocalrandom.current ();                 Rand.nextint (4, +);  

Generating pseudo-random numbers between 4~64

Summary of common functions of math class in Java

Related Article

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.