一、使用Math.random()
函数
(int)(((Math.random()*9+1))*100000)
说明:
1.Math.random()随机生成 [0.0,1.0) 的double
2.(Math.random()*9+1)*100000 的范围就是 ([0.0,1.0)*9+1)*100000 即 [100000.0,1000000.0)
二、使用new Random()
new Random().nextInt(900000) + 100000
说明:
1.new Random().nextInt(900000) 的范围是[0,900000)那么+100000后范围是[100000,1000000)
注:
Math.random()其实也是使用的new Random(),用的是random的nextDouble()方法,它的底层代码为:
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
private static final class RandomNumberGeneratorHolder {
static final Random randomNumberGenerator = new Random();
}