一个关于均匀取数的问题! 问题是这样:对于一组数0...800(例如)给定一个百分比,20%(例如)如果获得这组数的均匀采样801*20%?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先算出这个数组的个数n,然后用n×20%得出取样后的数据个数m。n÷m即为取值间隔数。每个间隔取一个数就可以了吧。 /* 把给定的数组随机排序的类 冰冻木马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+"次的取样"); }}//已经测试,请看看能否满足你的要求。 /* 把给定的数组随机排序的类 冰冻木马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然后编译、运行,看看能否满足你的要求。 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; } } 高分球算法--求算法分割字符串为数组 面试时遇到一些考基础的逻辑问题!如何提高这方面的能力! 有一道AWT的题,解不出,郁闷 JTextField 输入字符长度 如何设定 (比如值允许输入6个字符) java远程连接数据库问题 FRAME窗口的标题左手边的图标怎样改? 超简单问题 急!与数据库连接,更新数据时产生错误,请各位帮帮忙吧! s7110110(铁) 快去看我帖子呀~~~~~~~~~~~~~~~~~ 哪位大虾能讲一下哈希表的应用? 有谁做过qrcode的程序??? jTable 的问题
把给定的数组随机排序的类
冰冻木马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+"次的取样");
}
}
//已经测试,请看看能否满足你的要求。
把给定的数组随机排序的类
冰冻木马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然后编译、运行,看看能否满足你的要求。
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;
}
}