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