Random number Java.util.Random and java.lang.Math.Random ()-javaoriginal June 05, 2015 13:24:40 http://blog.csdn.net/Scryhuaihuai/article/details/46375693
First, Java.util.Random
The random class has two construction methods: Random () (using system time as a seed) and random (long seed). The constructor method simply creates a random number generator, and you must call the generator's method to produce a random number. The common methods of random are:
1.NEXTINT (): Returns a pseudo-random number of type int, with the value of the pseudo-random number within the int range.
2.nextInt (int N): Returns a pseudo-random number of type int, the value of the pseudo-random number is between [0,n].
Random random=new Random();int num1=random.nextInt(10);//生成一个[0,10)的随机数int num2=1+random.nextInt(10);//生成一个[1,10]的随机数
3.nextLong (): Returns a long type of pseudo-random number, and the value of the pseudo-random number is within the range of long.
4.nextFloat (): Returns a pseudo-random number of type float, with the value of the pseudo-random number between [0.0f,1.0f].
5.nextDouble (): Returns a pseudo-random number of type double, with the value of the pseudo-random number between [0.0d,1.0d].
6.nextBoolean (): Returns a Boolean pseudo-random number with a value of true or false for pseudo-random numbers.
Second, java.lang.Math.Random ()
It is a method, and Java.util.Random is a class.
public static double Random () returns a double with a positive number that is greater than or equal to 0.0 and less than 1.0. The return value is a pseudo-random number that is evenly distributed within the range (approximate).
double num1=Math.random(); //生成[0.0d,1.0d)的随机数int num2=(int)(Math.random()*10); //生成[0,10)的随机数int num3=(int)(1+Math.random()*10);//生成的[1,10]随机数
Random number Java.util.Random and java.lang.Math.Random ()-java