package stolen;   
import java.io.*;
public class Stolen {
/**
 * 
 */
private int  number=9000;    //生成个数
private long random[]=new long[this.number];
    public long Create()
    {
     return (long)(Math.random()*9000+1000);   ///*10 1位数,*100 2位数  ...
        
    }
    public void Write()
    {
       File file=new File("c:/random.txt");      
    }
    public void isSame(int i)
    {
     boolean blean=false;
         for(int k=0;k<i;k++)
         {
          
           if(this.random[i]==this.random[k])
           {
            this.random[i]=this.Create();
            blean=true;          
           }
           if(blean)
           {
            break;
           }
         }
         if(blean)
         {
          this.isSame(i);
         }
           
    }
    public static void main(String args[])
    {
     Stolen stolen=new Stolen();
    
     for(int i=0;i<stolen.number;i++)
        {
         stolen.random[i]=stolen.Create();
         if(i>0)
         {
        
            stolen.isSame(i);
         }       
        }
        for(int k=0;k<stolen.number;k++)
     {
     System.out.println(stolen.random[k]);
     }
        System.out.println(stolen.random.length);
    }
}

解决方案 »

  1.   

    number=8994时勉强可以出结果,9000时就不行了,是我的程序问题,还是默认堆栈大小不够?
      

  2.   

    这样应该可以,你试试吧public class myrandom {
    public static void main(String[] args) {
    // 先生成1000~9999的随机数,保证唯一
    int [] array = new int[9000];
    int i = 0;

    //赋初值,1000~9999
    for(i=0; i<array.length; i++)
    array[i] = i+1000;

    //每次生成一个随机数r,交换array[r]和array[i]
    for(i=array.length-1; i>=0; i--)
    {
    //生成[0,i]之间的随机数,当做下标
    int r = (int) (Math.random()*(i+1));
    //交换 array[r] 和 array[i]
    int tmp = array[r];
    array[r] = array[i];
    array[i] = tmp;
    }

    for(int e : array)
    System.out.printf("%d ", e);
    }
    }