import java.util.*;
 class Test{
public static void main(String args[]){   
for (int k=1;k<=9;k++){
    Random rand=new Random();
int i=rand.nextInt(99);
        System.out.println(i);
        }}
}为什么它产生的结果是同一个数。
但是把Random rand=new Random();放在for循环外面就对了
请大家说说原因

解决方案 »

  1.   

    import java.util.*;
    import java.lang.Thread;class Test{ public static void main(String args[])
    throws Exception
    {
    for (int k=1;k<=9;k++)
    {
         Random rand=new Random();
         Thread.sleep(100);
    int i=rand.nextInt(99);
            System.out.println(i);
            } }
    }
      

  2.   

    Two Random objects created within the same millisecond will have the same sequence of random numbers. 你也可以用 : Random rd = new Randon(long seed)