Original http://blog.csdn.net/chichoxian/article/details/20923501
A summary of some of the more common mathematical formulas in Java:
Rounding, returns the largest integer less than the target function, and returns 2 as followsMath.floor (-1.8);Rounding, the smallest integer that returns the number of development targetsMath.ceil ()Rounding rounded roundingMath.Round ()//calculates square root math.sqrt () //compute cube root Span class= "hljs-built_in" >MATH.CBRT () //returns the n power of Euler number e Math.exp (3); math.pow (3,2) ; //calculates the natural logarithm math.log (); math.abs (); Calculates the maximum value math.max (2.3,4.5); //calculates the minimum math.min (,); math.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;PublicClass Randomtest {/** * @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 with 16 elements of the array System.out. println (arrays.tostring (buffer)); System.out. println ( "rand.nextdouble ()" + rand.nextdouble ()); System.out. println ( "float floating point" + rand.nextfloat ()); System.out. println ( "Rand.nextgaussian" + Rand.nextgaussian ()); System.out. println ( "+ Rand.nextint ()); Produces a random integer system.out between 0~32. 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 (); int val = rand.nextint (4, +);
Generating pseudo-random numbers between 4~64
Public classMathdemo { Public Static voidMain (String args[]) {/*** ABS seek absolute value*/System.out.println (Math.Abs (-10.4));//10.4System.out.println (Math.Abs (10.1));//10.1 /*** Ceil ceiling means to return a large value, note some special values*/System.out.println (Math.ceil (-10.1));//-10.0System.out.println (Math.ceil (10.7));//11.0System.out.println (Math.ceil (-0.7));//-0.0System.out.println (Math.ceil (0.0));//0.0System.out.println (Math.ceil (-0.0));//-0.0 /*** floor meaning is to return a small value*/System.out.println (Math.floor (-10.1));//-11.0System.out.println (Math.floor (10.7));//10.0System.out.println (Math.floor (-0.7));//-1.0System.out.println (Math.floor (0.0));//0.0System.out.println (Math.floor (-0.0));//-0.0 /*** Max Two returns a large value, min and it instead, it's not an example.*/System.out.println (Math.max (-10.1,-10));//-10.0System.out.println (Math.max (10.7, 10));//10.7System.out.println (Math.max (0.0,-0.0));//0.0 /*** Random gets a number greater than or equal to 0.0 less than or equal to 1.0*/System.out.println (Math.random ()); //0.08417657924317234System.out.println (Math.random ());//0.43527904004403717 /*** Rint rounded, return double value * Note. 5 will take an even number*/System.out.println (Math.rint (10.1));//10.0System.out.println (Math.rint (10.7));//11.0System.out.println (Math.rint (11.5));//12.0System.out.println (Math.rint (10.5));//10.0System.out.println (Math.rint (10.51));//11.0System.out.println (Math.rint (-10.5));//-10.0System.out.println (Math.rint (-11.5));//-12.0System.out.println (Math.rint (-10.51));//-11.0System.out.println (Math.rint (-10.6));//-11.0System.out.println (Math.rint (-10.2));//-10.0 /*** Round rounding, float returns int value, double returns Long value*/System.out.println (Math.Round (10.1));//TenSystem.out.println (Math.Round (10.7));// OneSystem.out.println (Math.Round (10.5));// OneSystem.out.println (Math.Round (10.51));// OneSystem.out.println (Math.Round (-10.5));//-10System.out.println (Math.Round (-10.51));//-11System.out.println (Math.Round (-10.6));//-11System.out.println (Math.Round (-10.2));//-10 } }
Java Math class