A summary of several commonly used mathematical formulas in Java:
//Round, return the maximum integer smaller than the objective function, as follows, return -2 Math.floor(-1.8); //Round, return the minimum integer of the development target number Math.ceil() //Round Math.round() //Calculate the square root Math.sqrt() //Calculate the cubic root Math.cbrt() //Return the n-power of the Euler number e; //Calculate the multiplier, below is the second power of 3 Math.pow(3,2); //Calculate the natural logarithm Math.log(); //Calculate the absolute value Math.abs(); //Calculate the maximum value Math.max(2.3,4.5); //Calculate the minimum value Math.min(,); //Return a pseudo-random number, which is greater than or equal to 0.0 and less than 1.0 Math.random
The Random class is specifically used to generate a pseudo-random number. It has two constructors: one uses the default seed (with the current time as the seed), and the other requires the seed of a long integer displayed by the programmer.
Random provides more ways to generate various pseudo-random numbers than Math's random() method.
eg
import java.util.Arrays; import java.util.Random; public class RandomTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Random rand = new Random(); System.out.println("Random Boolean" + rand.nextBoolean()); byte[] buffer = new byte[16]; rand.nextBytes(buffer); //Produce a random array of numbers containing 16 array elements System.out.println(Arrays.toString(buffer)); System.out.println("rand.nextDouble()" + rand.nextDouble()); System.out.println("Float float" + rand.nextFloat()); System.out.println("rand.nextGaussian" + rand.nextGaussian()); System.out.println("" + rand.nextInt()); //Produce a random integer between 0 and 32 System.out.println("rand.nextInt(32)" + rand.nextInt(32)); System.out.println("rand.nextLong" + rand.nextLong()); } }In order to avoid the same sequence of numbers from two Random objects, it is usually recommended to use the current time as the seed of the Random object. The code is as follows:
Random rand = new Random(System.currentTimeMillis());
ThreadLocalRandom was introduced in java7
The way to use ThreadLocalRandom in multithreading is basically similar to using Random. The following program and fragment demonstrates the usage of ThreadLocalRandom:
First use current() to generate a random sequence and then use nextCXxx() to generate the desired pseudo-random sequence:
ThreadLocalRandom trand= ThreadLocalRandom.current(); int val = rand.nextInt(4,64);
Generate pseudo-random numbers between 4 and 64
The above is the full content of the summary of commonly used functions based on the Math class in Java brought to you by the editor. I hope it will be helpful to you and support Wulin.com more~