1. Random class overviewThis class is used to generate random numbers if two random instances are created 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 MethodPublic random (): Creates a new random number generator, does not give the seed, the default seed is the millisecond value of the current time public random (long Seed): Creates a new random number generator with a single long seed, given the seed, each time the resulting random number is is the same.
3. Random class member Methodpublic int Nextint (): Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator. public int Nextint (int n): Returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (not included) from this random number generator sequence.
ImportJava.util.Random; Public classRandomDemo01 { Public Static voidMain (string[] args) {Random R1=NewRandom (); for(intx = 0; x < 10; X + +) { intNUM1 = R1.nextint (100) + 1; System.out.print (NUM1+ ","); } System.out.println (); System.out.println ("---------------------------"); //given the seed, each time the resulting random number is the same. Random r2 =NewRandom (1111); for(intx = 0; x < 10; X + +) { intnum2 = R2.nextint (100) + 1; System.out.print (num2+ ","); } }}
Output Result:
First time execution:
81,34,92,11,52,1,95,44,7,42,---------------------------27,7,6,70,51,28,26,5,99,17,
Second execution:76,80,61,61,29,92,19,16,36,51,---------------------------27,7,6,70,51,28,26,5,99,17,
Java Api--random Class