The construction method of the Random class:
Public Random (); // This constructor uses a number related to the relative time of the current system time as the number of seeds.
Public Random (long seed); // you can specify the number of seeds to create.
Method:
Public boolean nextBoolean ();
Public double nextDouble (); // generates a random double value, between 0 and 1.0)
Public int nextInt (); // range between int-231-231-1
Public int nextInt (int n); // [0, n)
Public void setSeed (long seed );
Random r = new Random ();
Double d1 = r. nextDouble ();
Double d2 = r. nextDouble () * 5; // [0, 5.0) means to increase the range by 5 times
Double d3 = r. nextDouble () * 1.5 + 1; // [1, 2.5) first generate a random number in the [0, 1.5) interval and Add 1.
Int n1 = nextInt (); // generates any integer
Int n2 = r. nextInt (10); // [0, 10)
N2 = Math. abs (r. nextInt () % 10); // [0, 10)
Int n2 = r. nextInt (n); // [0, n)
N2 = Math. abs (r. nextInt () % n );
Int n3 = r. nextInt (11); // [0, 10]
N3 = Math. abs (r. nextInt () % 11); // [0, 10]
Int n4 = r. nextInt (18)-3; // [-3, 15)
N4 = Math. abs (r. nextInt () % 18)-3;
The following java program demonstrates some basic operations:
Package nemo;
Import java. util .*;
Public class lala {
Public static void main (String [] args)
{
Random r = new Random ();
Int n2 = r. nextInt (10); // [0, 10)
Int n3 = r. nextInt (11); // [0, 10]
Int n1 = r. nextInt (18)-2; // [-2, 16)
System. out. println (n2 + "+ n3 +" "+ n1 );
Boolean b1 = r. nextBoolean ();
Boolean b2 = r. nextBoolean ();
System. out. println (b1 + "" + b2 );
Double d1 = r. nextDouble (); // [0, 1.0)
Double d2 = r. nextDouble () * 5; // [0, 5.0)
Double d3 = r. nextDouble () * 1.5 + 1; // [1, 2.5)
System. out. println (d1 + "" + d2 + "" + d3 );
}
}