Import Java.util.random;public class Randomnumber{public static void Main (string[] args) {// Generate random number System.out.println ("Math.random ():" + math.random ()) using Java.lang.Math's Random method;// Constructs a Java.util.Random object System.out.println ("Random object constructed using a constructor with no parameters:") using a construction method that does not have a number of parameters; Random Rd1 = new Random (),//Generates integer SYSTEM.OUT.PRINTLN ("int:" + rd1.nextint ()) for various types of stochastic//by uniform distribution;// Produces a long integer System.out.println ("Long:" + Rd1.nextlong ()) by a uniform distribution, which produces a float number greater than or equal to 0, less than 1 [0, 1] System.out.println ("float:" + rd1.nextfloat ());//The Double number System.out.println ("Double:" + rd1.nextdouble ()) that produces a range of [0, 1] evenly distributed;// Generate random number System.out.println ("Gaussian:" + Rd1.nextgaussian ()) according to normal distribution;//generate a series of random number System.out.print ("Random Integer Sequence:"); for (int i = 0; I < 5; i++) {System.out.print (Rd1.nextint () + "");} System.out.println ();//Specifies the range System.out.print ("[0,10)" In the range of the random number produced by the random Integer sequence: "); for (int i = 0; I < 10; i++) {//the nextint (int n) method of the random returns a random number System.out.print (Rd1.nextint (10) + "") in the range [0, n];} System.out.println (); System.out.print ("[5,23) in the range of random integer Sequences:"); for (int i = 0; I < 10; i++) {//Because the range of the nextint (int n) method starts at 0,//So the interval [5,28] needs to be converted to 5 + [0, 23]. System.out.print (5 + rd1.nextint (23) + ""); System.out.println (); System.out.print ("Using nextfloat () to generate a sequence of random integers in the [0,99) Range:"); for (int i = 0; I < 10; i++) {System.out.print ((int) (Rd1.nextfloat () * 100) + ""); System.out.println (); System.out.println ();//construction of the random object using a constructor with a parameter//the parameter of the constructor is a long type, which is the seed for generating the random number. System.out.println ("Random object constructed with the constructor method with a number of parameters:"); Random ran2 = new random (10);//for seeds the same randomness object, the generated sequence is the same. SYSTEM.OUT.PRINTLN ("Generates a random integer sequence in [0,10] using a random object of seed 10:"); for (int i = 0; I < 10; i++) {System.out.print (Ran2.nextint (10) + "");} System.out.println (); Random ran3 = new random (10); SYSTEM.OUT.PRINTLN ("Generates a random integer sequence in [0,10] using a random object with a seed of 10:"); for (int i = 0; I < 10; i++) {System.out.print (Ran3.nextint (10) + "");} System.out.println ();//The sequence of random numbers generated by ran2 and Ran3 is the same, assuming that there is no random object generated using the two no-parameter constructors,//There is no such case. This is due to the fact that the seed default of the random object generated in the no-parameters constructor is the number of milliseconds of the current system time. In addition, the direct use of random cannot avoid generating repeated numbers, assuming the need to generate a sequence of random numbers that do not repeat, with the aid of arrays and collection classes}} to execute the results:C:/>java randomnumbermath.random (): 0.525171492959965 random objects constructed using a constructor with no parameters: Int:636539740long:- 752663949229005813float:0.87349784double:0.4065973309853902gaussian:0.4505871918488808 random integer sequence: 1936784917 1339857386-1185229615 1883411721 1409219372[0,10) range of random integer sequences: 1 1 5 5 9 0 1 0 2 4[5,23) in the range of random integer sequences: 9 13 26 18 11 27 26 12 21 8 using Nextfloat () to generate a sequence of random integers in the range of [0,99]: 1 47 72 59 49 86 80 88 55 82 random objects constructed with the constructor method of the parameter: generates a sequence of randomly integers in [0,10] using a random object with a seed of 10:3 0 3 0 6 6 7 8 1 4 Use a random object that has a seed of 10 to generate a sequence of stochastic integers in [0,10]: 3 0 3 0 6 6 7 8 1 4
Java random function Usage method random