生成随机数是一个常见的功能,例如我们给用户生成随机红包。但是你知道java.util.Random类在高并发的情况下具有性能问题和安全问题吗?我们在高并发常见下应该如何生成随机数呢?
要知道为什么java.util.Random类具有性能问题,得深入到其中是实现来探究一下。
首先看一下Random类的doc说明
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula.
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers
Random类的实例可以用来生成一个“伪随机”数字流。这个类使用48bit的种子,并且使用