1. Random class
1 Public class Random extends implements Serializable;
An instance of this class is used to generate a pseudo-random number stream. This class uses 48-bit seeds.
(1) Random class overview
• This class is used to generate 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) How random is constructed
Public Random (): No seed is used, is the default seed, is the millisecond value of the current time
• Public Random (Long Seed): to a fixed seeding seed
Seed: The random number of a general computer is a pseudo-random number, with a true random number (seed) as the initial condition, and then iterative generation of random numbers with a certain algorithm.
(3) Random member method
1 Public int nextint (): Returns a random number in the range of int 2public int Nextint (int n): Returns a random number in the range of [0,n]
(4) Code example:
1 Packagecn.itcast_01;2 3 ImportJava.util.Random;4 5 /*6 * Random: A class that generates random numbers7 * 8 * Construction Method:9 * Public Random (): No seed, with default seed, is the millisecond value of the current timeTen * Public Random (long Seed): gives the specified seed One * A * Once the seed is given, the random number obtained is the same each time. - * - * Member Method: the * public int nextint (): Returns a random number in the range of int - * public int nextint (int n): Returns the internal random number of the range of [0,n] - */ - Public classRandomdemo { + Public Static voidMain (string[] args) { - //Creating Objects + //random R = new Random (); ARandom r =NewRandom (1111); //Given seed, the random number returned each time is the same at - for(intx = 0; x < 10; X + +) { - //int num = R.nextint (); - intnum = r.nextint (100) + 1; - System.out.println (num); - } in } -}
Java Basic Knowledge Hardening 83:random class overview and method usage