English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Text
//Summary of several commonly used mathematical formulas in Java:-2 Round to the nearest integer, return the largest integer less than the target function, as follows it will return-1.8Math.floor( //); Round to the nearest integer, return the smallest integer of the development target number //Math.ceil() Round to the nearest integer //Math.round() Calculate the square root //Math.sqrt() Calculate the cube root //Math.cbrt() Returns the nth power of Euler's number e3); //Math.exp(3Calculate the power, the following is the calculation of2power 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(,); //returns a pseudo-random number, which is greater than or equal to 0.0 and less than1.0 Math.random
The Random class is specifically used to generate a pseudo-random number, it has two constructors: one constructor uses the default seed (the current time as the seed), and the other constructor requires the programmer to explicitly pass a long integer seed.
The Random class provides more ways to generate various pseudo-random numbers than the Math.random() method.
e.g
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 number" + rand.nextBoolean()); byte[] buffer = new byte[16]; rand.nextBytes(buffer); //Generate an array containing16An array of random numbers of elements System.out.println(Arrays.toString(buffer)); System.out.println("rand.nextDouble()"} + rand.nextDouble()); System.out.println("Float floating-point number" + rand.nextFloat()); System.out.println("rand.nextGaussian" + rand.nextGaussian()); System.out.println("" + rand.nextInt()); //generate a 0~32random integer between System.out.println("rand.nextInt(32)" + rand.nextInt(32)); System.out.println("rand.nextLong" + rand.nextLong()); } }
To avoid two Random objects generating the same number sequence, it is usually recommended to use the current time as the seed of the Random object, as shown in the following code:
Random rand = new Random(System.currentTimeMillis());
in java7where ThreadLocalRandom is introduced
The way to use ThreadLocalRandom in a multi-threaded environment is similar to using Random, as the following program snippet demonstrates the usage of ThreadLocalRandom:
Firstly, 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);
generate4~64pseudo-random numbers between
This is the full summary of commonly used functions of the Math class in Java that the editor has brought to you. I hope it will be helpful to everyone, and please support the Yell Tutorial~