对比两种写法:
第一种:
public static void main(String args[]){Random random = new Random(System.currentTimeMillis());for(int i=0; i<20; i++){int sindex = random.nextInt(2);System.out.println(sindex);}}
第二种:
public static void main(String args[]){for(int i=0; i<20; i++){Random random = new Random(System.currentTimeMillis());int sindex = random.nextInt(2);System.out.println(sindex);}}
第一种中产生的随机数是正常的,然而在第二种写法中,所得随机数都一样。暂不知为何,初步猜测与系统时间相关。