// randomly generates an integer between 1~100 int randomnumber = (int) (Math.random () * +) + 1; System.out.println (randomnumber);
This is a method of generating random numbers directly using the Math.random () method.
There is also a way to use the random class.
Random (): Creates a new random number generator.
Random (Long Seed): Creates a new random number generator with a single long seed.
The first method of construction is to use the default current system time as the number of milliseconds to seed: random r1 = new Random ();
Random random = new random ();
int randomnumber1= random.nextint (100);
The second method is to use the number of seeds that you specify
Random random1 = new random (100);
for (int i = 0; i < i++) {
System.out.print (Random1.nextint (10) + "");
}
It is found that as long as the number of seeds is consistent with the parameters in Nextint (), the random number generated each time is the same (so this is a pseudo-random number).
System.out.println ("\ n uses the same seed to generate random numbers as follows:");
Random random2 = new random (100);
for (int i = 0; i < i++) {
System.out.print (Random2.nextint (10) + "");
}
As follows:
How to use the random () class for Java