Java api ---- Math class and Random class, api ---- mathrandom
Math class:
The Math class in the java. lang package is also called the mathematics class, which is a tool class. It contains many class methods used for scientific computing, involving most mathematical functions. These methods can be called directly by class names. In addition, two static mathematical constants Math. PI and Math. E are provided, respectively, representing E and PI.
The following are common methods:
Int abs (int I) is used to calculate the absolute value of an integer (In addition, the methods for long, float, and double)
Double ceil (double d) returns the smallest integer less than d.
Double floor (double d) is not greater than the smallest integer of d
Int max (int i1, int i2) is used to calculate the maximum integer between two integers (and the methods for long, float, and double)
Int min (int i1, int i2) is used to calculate the minimum integer between two integers (and the methods for long, float, and double)
Double random () produces 0 ~ Random number between 1
Int round (float f) returns the integer closest to f.
Double sqrt (double a) returns the square root
Random class:
The Random algorithm implemented in the Random class is pseudo-Random, that is, Random with rules. When a random algorithm is executed, the numbers of the origins of the random algorithm are called seed. A certain number is transformed based on the number of seeds to generate the required random numbers.
For a Random object with the same number of seeds, the Random numbers generated by the same number of times are identical. That is to say, two Random objects with the same number of seeds have identical Random numbers generated for the first time and identical Random numbers generated for the second time. This requires special attention when generating multiple random numbers.
The following describes how to use the Random class and how to generate a Random array of the specified range and the probability of implementing the requirements in the program.
1. the Random class contains two constructor methods, which are described in sequence below:
A. public Random () This constructor uses a number related to the relative time of the current system time as the seed number, and then uses this seed number to construct the Random object.
B. public Random (long seed) This constructor can be created by creating a number of seeds.
Sample Code:
Random r = new Random ();
Random r1 = new Random (10 );
Again, the number of seeds is only the source number of the random algorithm, and is irrelevant to the interval of the generated random number.
2. Method:
A, public boolean nextBoolean ()
This method is used to generate a random boolean value. The probability of true and false values generated is equal, that is, the probability is 50%.
B. public double nextDouble ()
This method generates a random double value between 0 and 1.0.
C, public int nextInt ()
The function of this method is to generate a random int value, which is between the int range, that is, between-231 and 231-1.
D. public int nextInt (int n)
The function of this method is to generate a random int value, which is an interval between [0, n), that is, a random int value between 0 and n, contains 0, not n.
E. public void setSeed (long seed)
This method is used to reset the number of seeds in the Random object. The Random object after the number of seeds is set is the same as the Random object created with the new keyword for the same number of seeds.