The random class is a pseudo-random number generator. They are called pseudo-random numbers (pseudorandom) because they are simply uniformly distributed sequences. The random class defines the following constructors:
Random ()
Random (Long Seed)
The random number generator created with the first version uses a relatively unique seed (the current time). The second version allows you to specify the seed manually. If you initialize the random object with a seed, the start point is defined for the random sequence. If you initialize another random object with the same seed, you get the same random sequence. If you want to generate a different sequence, you need to specify a different seed. One way to achieve this is to use the current time as the seed of the random object. This approach reduces the likelihood of repeating sequences.
The core public method defined by the random class:
return value |
Method |
Describe |
Boolean |
Nextboolean () |
Returns the next Boolean random number |
void |
Nextbytes (byte vals[]) |
Populate Vals with randomly generated values |
Double |
Nextdouble () |
Returns the next double type random number |
Float |
Nextfloat () |
Returns the next float type random number |
Double |
Nextgaussian () |
Returns the next Gaussian distribution random number |
Int |
Nextint () |
Returns the next int type random number |
Int |
Nextint (int n) |
Returns the next int type random number between 0 and N |
Long |
Nextlong () |
Returns the next long type random number |
void |
Setseed (Long Newseed) |
Set the value specified by Newseed to seed |
You can extract multiple types of random numbers from the random object, which can be obtained by Boolean, Byte, Integer, and float. In addition, the Nextgaussian () method returns a standard Gaussian distribution random number with a mean of 0.0 and a variance of 1.0, with a type of double, which is known as the bell curve. The following example shows a random sequence produced by the Nextgaussian () method. First, 100 random Gaussian values are obtained and their averages are calculated. Then, in 0.5 units, the number of values that fall within the range of plus and minus two standard deviations is counted.
Java learning about the random number tool class--random class