Random Class
1. Classes for generating random numbers
If you create two Random instances with the same seed, the same method call sequence is made for each instance, and they will generate and return the same sequence of numbers.
2. Construction Method:
A:random (): Creates a new random number generator. This construction method sets the seed of the random number generator to a value that is completely different from the value that is used by all other invocations of this construction method.
The seed is not given, with the default seed, which is the millisecond value of the current time , different from the random number generated each time
B:random (Long Seed): Creates a new random number generator with a single long seed. The seed is the initial value of the internal state of the pseudo-random number generator, which can be maintained by means of protected int next (int bits).
The seed is specified, and the random number is the same for each seed
3. Member Methods:
A:int Nextint () returns the random number in the INT range
The Random class implements the Nextint method as follows:
public int Nextint () {
Return next (32);
}
B:int nextint (int n) returns the random number in the range of [0,n]
Public class Randomdemo {
Public staticvoid main (string[] args) {
// Creating Objects
//Random R = new Random (); // no seed given, different random number produced at a time
random R = new random (1111); given seed, the same random number is generated each time
for ( int x = 0; x < 10; x++) {
//int num = R.nextint ();//int Span style= "Font-family:consolas" >
intnum = r. Nextint (+ 1); random number in [1,100]
System. out. println (num);
}
}
}
Java_ Common Class 09_random class