问题是这样:
对于一组数0...800(例如)给定一个百分比,20%(例如)
如果获得这组数的均匀采样801*20%?
谢谢

解决方案 »

  1.   

    先算出这个数组的个数n,然后用n×20%得出取样后的数据个数m。n÷m即为取值间隔数。每个间隔取一个数就可以了吧。
      

  2.   

     /* 
     把给定的数组随机排序的类 
       冰冻木马QQ57629776 Email:[email protected] 2007年12月
    */
    public class ArraryToRand
    {
    int [] toRandArrary;
    ArraryToRand(final int arrary[]) //会引起原来数组的更新
    {
    int temp=0,j;
    toRandArrary=arrary; for(int i=0;i<arrary.length;i++) //随机交换数组元素,使数组随机排序;
    {
    j=(int)(Math.random()*toRandArrary.length);
    temp=toRandArrary[i];
    toRandArrary[i]=toRandArrary[j];
    toRandArrary[j]=temp;
    }
    }
    /*
    供程序测试用
    */
        //以下是1-800的有序数,经过以上类的排序后将成为无序数组
    public static void main(String [] args)
    {
    int [] a=new int[800]; //分配一个800的整形数组
    for(int i=0;i<a.length;i++)
    a[i]=i+1;
    int tem1=0,tem2=0,tem3=0,tem4=0,tem5=0,tem=0,count=0;
    System.out.println("随机取样中...\n"
                        + "请耐心等待(如果运气不好,可能要等很久哦!)\n"
    +"友情提示:可按Ctrl+C终止程序然后多测试几次,有时只要一秒钟就可以的哦!");
    while(true)
    {
    ArraryToRand myarrary=new ArraryToRand(a);
    for(int i=0;i<160;i++)
    {
    tem1=tem1+a[i];//取得0-159号数的和
    tem2=tem2+a[i+160];//取得160-319号数的和
    tem3=tem3+a[i+160*2];
    tem4=tem4+a[i+160*3];
    tem5=tem5+a[i+160*4];
    }
    if(
    (tem1-tem2)<=800*0.2 //看0-159号数的和 是否与 160-319号数是否相差过大,如果过大则生重先排序取样
    &&(tem1-tem3)<=800*0.2
    &&(tem1-tem4)<=800*0.2
    &&(tem1-tem5)<=800*0.2
    &&(tem2-tem3)<=800*0.2
    &&(tem2-tem4)<=800*0.2
    &&(tem2-tem5)<=800*0.2
    &&(tem3-tem4)<=800*0.2
    &&(tem3-tem5)<=800*0.2
    &&(tem4-tem5)<=800*0.2
    ) break;//如果取样成功,则退出程序
    else
    count++;

    }
    for(int i=0;i<a.length;i++)
    System.out.println(a[i]);
    System.out.println("共进行了"+count+"次的取样");
    }
    }
    //已经测试,请看看能否满足你的要求。
      

  3.   

     /* 
     把给定的数组随机排序的类 
       冰冻木马QQ57629776 Email:[email protected] 2007年12月
    */
    public class ArraryToRand
    {
    int [] toRandArrary;
    ArraryToRand(final int arrary[]) //会引起原来数组的更新
    {
    int temp=0,j;
    toRandArrary=arrary; for(int i=0;i<arrary.length;i++) //随机交换数组元素,使数组随机排序;
    {
    j=(int)(Math.random()*toRandArrary.length);
    temp=toRandArrary[i];
    toRandArrary[i]=toRandArrary[j];
    toRandArrary[j]=temp;
    }
    }
    /*
    供程序测试用
    */
        //以下是1-800的有序数,经过以上类的排序后将成为无序数组
    public static void main(String [] args)
    {
    int [] a=new int[800]; //分配一个800的整形数组
    for(int i=0;i<a.length;i++)
    a[i]=i+1;
    int tem1=0,tem2=0,tem3=0,tem4=0,tem5=0,tem=0,count=0;
    System.out.println("随机取样中...\n"
                        + "请耐心等待(如果运气不好,可能要等很久哦!)\n"
    +"友情提示:可按Ctrl+C终止程序然后多测试几次,有时只要一秒钟就可以的哦!");
    while(true)
    {
    ArraryToRand myarrary=new ArraryToRand(a);
    for(int i=0;i<160;i++)
    {
    tem1=tem1+a[i];//取得0-159号数的和
    tem2=tem2+a[i+160];//取得160-319号数的和
    tem3=tem3+a[i+160*2];
    tem4=tem4+a[i+160*3];
    tem5=tem5+a[i+160*4];
    }
    if(
    (tem1-tem2)<=800*0.2 //看0-159号数的和 是否与 160-319号数是否相差过大,如果过大则生重先排序取样
    &&(tem1-tem3)<=800*0.2
    &&(tem1-tem4)<=800*0.2
    &&(tem1-tem5)<=800*0.2
    &&(tem2-tem3)<=800*0.2
    &&(tem2-tem4)<=800*0.2
    &&(tem2-tem5)<=800*0.2
    &&(tem3-tem4)<=800*0.2
    &&(tem3-tem5)<=800*0.2
    &&(tem4-tem5)<=800*0.2
    ) break;//如果取样成功,则退出程序
    else
    count++;

    }
    for(int i=0;i<a.length;i++)
    System.out.println(a[i]);
    System.out.println("共进行了"+count+"次的取样");
    }
    }
    //请直接把以上文件保存为ArraryToRand.java然后编译、运行,看看能否满足你的要求。
      

  4.   

    import java.util.Set;
    import java.util.TreeSet;public class GetNumber {
        public static void main(String[] args) {
         System.out.println(getNumbers(0, 800, (int)((800-1+1) * 0.20)));
        }    static Set<Integer> getNumbers(int minNum, int maxNum, int account){
          int len = maxNum - minNum;
        
         // 获取数据
         Set<Integer> nums = new TreeSet<Integer>();
         while (nums.size() < account) {
    int r = (int) (Math.random() * len + 1);
    nums.add(new Integer(minNum + r));
    }
        
         return nums;
        }
           }