In java, we can obtain a random number (generating a random number) in two ways. lang. math. random () static method, the other is to create java. util. random object. The following describes how to use these two methods:
1. java. lang. Math. random ()
When using this static method, we do not need to import any packages, because the java. lang. * package is loaded by default. The following example shows:
[Java]
Package com. luiszhang. test;
Public class RandomTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Random () automatically generates a 0.0-1.0 double-precision random number.
Double doubleRadomNum = Math. random ();
System. out. println (doubleRadomNum );
// Generate a zero-double-precision Random Number
DoubleRadomNum = Math. random () * 1000;
System. out. println (doubleRadomNum );
// Generate an integer random number ranging from 0
Int intRandomNum = (int) (Math. random () * 1000 );
System. out. println (intRandomNum );
}
}
2. Create a java. util. Random object
[Java]
Package com. luiszhang. test;
Import java. util. Random;
Public class RandomTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
// Create a random object
Random random = new Random ();
// Obtain an integer Random Number
Int intNumber = random. nextInt ();
// Obtain a random floating point number (0-1)
Float floatNumber = random. nextFloat ();
// Obtain the double-precision random number (0-1)
Double doubleNumber = random. nextDouble ();
// Obtain a boolean Random Number
Boolean booleanNumber = random. nextBoolean ();
System. out. println ("intNumber:" + intNumber );
System. out. println ("floatNumber:" + floatNumber );
System. out. println ("doubleNumber:" + doubleNumber );
System. out. println ("booleanNumber:" + booleanNumber );
}
}
Note:
(1) We can use System. currentTimeMillis () method. This method returns a millisecond value from 00:00:00, January 1, January 1, 1970 to the present. The return type is long. We can use it as a random number, and we can use it to modulo some numbers, you can limit it to a single range. (Duplicate values are easy to generate in a loop, which is not easy to use)
(2) Random random = new Random (); if the base number is not specified, a Random number is returned, and the result is different each time. Of course, we can also specify the base number, for example: Random random = new Random (100); the Random number sequence generated by the same base number is the same.
Below we will write a test program to verify these:
[Java]
Package com. luiszhang. test;
Import java. util. Random;
Public class RandomTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
// Create two random numbers based on 100
Random randomNumber1 = new Random (1, 100 );
Random randomNumber2 = new Random (100 );
// Verify whether the Random numbers generated by the Random object generated by the same base number are equal
For (int I = 0; I <5; I ++ ){
System. out. print (randomNumber1.nextInt () + "");
System. out. print (randomNumber2.nextInt () + "\ n ");
System. out. println ("Is equal is" + (randomNumber1.nextInt () = randomNumber2.nextInt ()));
System. out. println ("------------------------------");
System. out. print (randomNumber1.nextDouble () + "");
System. out. print (randomNumber2.nextDouble () + "\ n ");
System. out. println ("Is equal is" + (randomNumber1.nextDouble () = randomNumber2.nextDouble ()));
System. out. println ("------------------------------");
System. out. print (randomNumber1.nextFloat () + "");
System. out. print (randomNumber2.nextFloat () + "\ n ");
System. out. println ("Is equal is" + (randomNumber1.nextFloat () = randomNumber2.nextFloat ()));
System. out. println ("------------------------------");
System. out. print (randomNumber1.nextBoolean () + "");
System. out. print (randomNumber2.nextBoolean () + "\ n ");
System. out. println ("Is equal is" + (randomNumber1.nextBoolean () = randomNumber2.nextBoolean ()));
System. out. println ("------------------------------");
}
// Generate a random number by yourself at the current time. the random number range is 1-32.
For (int I = 0; I <5; I ++ ){
System. out. print (System. currentTimeMillis () % 32 + 1) + "\ n ");
System. out. println ("------------------------------");
}
}
}