Summary of common functions based on math class in Java

Summary of several commonly used mathematical formulas in Java:

Random class is specially used to generate a pseudo-random number. It has two constructors: one constructor uses the default seed (taking the current time as the seed), and the other constructor needs the programmer to display the seed passed in a long integer.

Random provides more ways to generate various pseudo-random numbers than math's random () method.

e.g

In order to prevent two random objects from generating the same number sequence, it is generally 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 Java 7

In the case of multithreading, the method of using threadlocalrandom is basically similar to that of using random. The following program fragments demonstrate the usage of threadlocalrandom:

First, use current() to generate a random sequence, and then use nextxxx() to generate the desired pseudo-random sequence:

Generate a pseudo-random number between 4 and 64

The above is all the content of the summary of common functions based on math class in Java brought by Xiaobian. I hope it can help you and support programming tips~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>