The random number of 1.java is pseudo-random, and in one interval the probability is generated. Use this to achieve probabilistic problems:
Achieve 60% chance to execute a program, 30% chance to execute B program, 10% chance to execute C program.
Random rnd = new Random ();d ouble d = rnd.nextdouble (); if (d<=0.6) {System.out.println ("execute a");} else if (d<=0.9) {System.out.println ("execute B");} Else{system.out.println ("Execute C");}
The number of seeds in 2.Random is the number of origins that generate random numbers, and is not related to what is generated. The same random number generated by random numbers with the same number of two seeds is the same, and the internal execution algorithm is consistent, and the application typically uses only one random object. However, the number of seeds for new random () is random, and the same random number generated by the two new random () is basically different.
The method in 3.Random nextbytes (byte[] bytes) attaches random bytes to bytes, with no return value.
4. A few examples:
Generate a random number between [ -3,15]:
System.out.println (Rnd.nextint (4) * ( -1) + rnd2.nextint (15)); System.out.println (Rnd.nextint (18)-3);
Generate a random number between [1,2.5]:
SYSTEM.OUT.PRINTLN (1 + rnd.nextdouble () * 0.5 + rnd.nextint (2)); System.out.println (rnd.nextdouble () * 1.5 + 1);
Generated. Random number between [0,5.0]:
System.out.println (rnd2.nextdouble () + rnd2.nextint (5)); System.out.println (Rnd2.nextdouble () * 5);
5.nextBoolean randomly generated true false,math.random () is called by the Random nextdouble ()
Java's random