Java random class usage details

Random class (Java. Util)

The random algorithm implemented in random class is pseudo-random, that is, regular random. In the process of random, the origin number of random algorithm is called seed. Based on the seed number, a certain transformation is carried out to generate the required random number.

For random objects with the same seed number, the random numbers generated by the same number of times are exactly the same. That is, for two random objects with the same seed number, the random numbers generated for the first time are exactly the same, and the random numbers generated for the second time are also exactly the same. This requires special attention when generating multiple random numbers.

The following describes the use of the random class, how to generate a random array of specified intervals, and the probability required in the program.

1. Generation of random objects

Random class contains two construction methods, which are described in turn below:

a、public Random()

This construction method uses a number related to the relative time corresponding to 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 construction method can be created by specifying a seed number.

Example code:

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
分享
二维码
< <上一篇
下一篇>>