JDK8 Online Api Chinese Manual
JDK8 Online Api English Manual
Random class
The Random class is a pseudo-random number generator. They are called pseudorandoms because they are simply uniformly distributed sequences. The Random class defines the following constructor:
Random()
Random(long seed)
The random number generator created with the first version uses a relatively unique seed. The second version allows manual seeding.
If a Random object is initialized with a seed, a starting point is defined for the random sequence. If you use the same seed to initialize another Random object, you will get the same random sequence. If you want to generate different sequences, you need to specify different seeds. One way to achieve this effect is to use the current time as the seed of the Random object. This approach reduces the possibility of getting repeated sequences.
Table 1 lists the core public methods defined by the Random class. These methods have been in Random for a long time (many have been provided since Java 1.0), and have been widely used.
method | Description |
---|---|
boolean nextBoolean() | Returns the next Boolean random number |
void nextBytes(byte vals[]) | Fill vals with randomly generated values |
double nextDouble() | Returns the next double random number |
float nextFloat() | Returns the next float random number |
double nextGaussian | Returns the next Gaussian random number |
int nextInt () | Returns the next int random number |
int nextInt (int n) | Returns the next int random number between 0 and n |
long nextLong() | Returns the next long random number |
void setSeed(long newSeed) | Set the seed (i.e. the starting point of the random number generator) to the value specified by newSeed |
It can be seen that various types of random numbers can be extracted from the Random object; random Boolean values can be obtained by calling the nextBoolean () method; random bytes can be obtained by calling the nextBytes () method; random integers can be extracted by calling the nextInt () method ; By calling the nextLong () method, you can obtain long integer random numbers evenly distributed throughout the value range. The nextFloat () and nextDouble () methods return float and double random numbers evenly distributed between 0.0 and 1.0, respectively. Finally, the nextGaussian () method returns a standard Gaussian pseudo-random number with a mean of 0.0 and a variance of 1.0. Its type is double, which is the bell curve.
The following is an example that demonstrates the random sequence generated by the nextGaussian () method. This example first obtains 100 random Gaussian values and calculates their average value. The program also uses 0.5 as a unit to categorize and count the number of values that fall within the range of plus or minus two standard deviations. The results are displayed graphically on the left side of the screen.
//Demonstrate random Gaussian values.
import java.util.Random;
class RandDemo {
public static void main(String[] args) {
Random r = new Random();
double val;
double sum = 0;
int bell[] = new int[10];
for (int i=0;i<100;i++){
val = r.nextGaussian();
sum+=val;
double t=-2;
for (int x=0;x<10;x++,t+=0.5){
if(val<t){
bell[x]++;
break;
}
}
}
System.out.println("Average of values: "+(sum/100));
//display bell curve,sideways
for (int i=0;i<10;i++){
for(int x=bell[i];x>0;x--){
System.out.print("*");
}
System.out.println();
}
/**
* 输出:可以看出,得到了类似钟形分布的数字
* ****
* ******
* *******
* ****************
* *******************
* ******************
* *************
* **********
* ***
* ****
*/
}
}
JDK8 adds three methods to the Random class that support the new streaming API: doubles (), ints (), and longs (). They all return a stream that contains a sequence of random numbers of the specified type. Each method defines several overloaded forms. The simplest forms of the three methods are listed below:
DoubleStream doubles()
IntStream ints()
LongStream longs()
The stream returned by the doubles () method contains a random number of type double (the value range is between 0.0 and 1.0). The stream returned by the ints () method contains a random value of type int. The stream returned by the long () method contains a random value of type long. For these three methods, the returned stream is actually infinite. They have several overloaded forms that allow you to specify the size, starting point, and upper bound of the stream.