Random构造方法
Random构造方法有两种,一种是有参,一种是无参,在计算机中本来就是没有真正存在的随机序列,都是伪序列,这些序列是由固定的算法来生成的,因此如果给它一个固定的值,得到的序列也就是相同的
代码分析
public static void main(String[] args) {
/**
* 无参的构造函数,每次生成的随机数的的值不同
*/
Random random = new Random();
for(int i = 0;i<10;i++) {
System.out.print(random.nextInt(100)+" ");
}
System.out.println();
/**
* 有参的构造方法,每次生成的随机数序列相同
*/
Random random1 = new Random(100);
for(int i = 0;i<10;i++) {
System.out.print(random1.nextInt(100)+" ");
}
System.out.println();
}
不论你运行多少次,第二次输出的随机数总是这个,只是序列改变而已,只有当你修改种子的值才会出现不同的随机数序列
2 31 7 35 8 50 30 17 82 2
15 50 74 88 91 66 36 88 23 13
Process finished with exit code 0