笔者最近在写段小程序,因功能需要写了以下这个函数
private String getrannumber(){
StringBuffer strbufguess=new StringBuffer();
String strguess=new String();
int []nums={0,1,2,3,4,5,6,7,8,9};
Random rannum=new Random();
int count;
int i=0,temp_i=0;
for (int j=10;j>4;j--){
//取1-10的不重复数
i=0;temp_i=0;
count=rannum.nextInt(j);
while(i<=count){
if (nums[temp_i]==-1) temp_i++;
else {
i++;temp_i++;
}
}
strbufguess.append(Integer.toString(nums[temp_i-1]));
//加数字已取的标志
nums[temp_i-1]=-1;
}
strguess=strbufguess.toString();
rannum=null;
strbufguess=null;
nums=null;
return strguess;
}