一个随机数问题,望高手帮忙!! 有 int count1、int count2,里面分别存放了一些id号,怎样在这些id号范围内,随机出100个不重复的随机数。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 LZ的问题不是很清楚啊,“int count1、int count2,里面分别存放了一些id号”能说的明白点么? 这是个考试系统分两种不同类型的题,我根据这些不同类型的题的标识,把他们的id号查出来,然后根据这些id号,取出试题的内容。所以要根据这些id号,随机出100道题。不知我说明白了没有? 不是数组,int count1=rs.getInt("id") count2-count1 得到随机的范围系数 aa * Math.random() 取整 再加上count1 得到一个随机题目的ID 重复100次 count2-count1 得到随机的范围系数 aid = count1id = id + Math.round(a / 100 * Math.random()) 重复98次 得到有顺序的ID号 //晕,你100个ID不能放一个变量里吧?这样吧,我假设你把这些id放在两个数组里:int[] counts1;int[] counts2;Random r = new Random(); int i = 0; int c; int[] count_result=new int[100];while(i<100){ int flag=r.nextInt(2); int[] tempCount; if(flag==1)tempCount=counts1; else tempCount=counts2; int n=tempCount.length; c=r.nextInt(n); if(tempCount[c]!=-1){ count_result[i]=tempCount[c]; if(flag==1)counts1[c]=-1; else counts2[c]=-1; i++; } }for(int j=0;j<100;j++){ out.println(count_result[j]);} 不能减吧,count1和count2,里面不是一个数,而是这样的count1=7382921041131221321411501591681771861952042132222312,都是id号。按照那样的方法取出的都是同一道题。 int[] a = new int[100];for(int i = 0 ; i < 100 ; i++){ a[i] = i;}int n = a.length;for(int i = 99 ; i >= 0 ; i--){ int temp = (int)(Math.random()*n--); System.out.println(a[temp]); a[temp] = a[i]; //把该次随机到的数的位置放最后一个数} 【新手】windows自身有java虚拟机吗 还是必须另外下载 input type="text"的问题 新手sqlserver2008测试出问题 求助 在2个框架之间一个框架内的单击事件可以给另一个框架的表单付值 JAVA如何分割字符串 实现登录人数统计的问题 JSPServeltContext对象理解问题? 菜鸟送分,问在JB下在jsp中调用applet的相对路径的写法。 请问大家,现在都用什么来配置jsp环境的呀? spring boot No qualifying bean of type javaBean 中使用的问题急 麻烦大家给个思路!!
a * Math.random() 取整 再加上count1 得到一个随机题目的ID 重复100次
id = count1
id = id + Math.round(a / 100 * Math.random()) 重复98次 得到有顺序的ID号
int[] counts1;
int[] counts2;
Random r = new Random();
int i = 0;
int c;
int[] count_result=new int[100];
while(i<100){
int flag=r.nextInt(2);
int[] tempCount;
if(flag==1)tempCount=counts1;
else tempCount=counts2;
int n=tempCount.length;
c=r.nextInt(n);
if(tempCount[c]!=-1){
count_result[i]=tempCount[c];
if(flag==1)counts1[c]=-1;
else counts2[c]=-1;
i++;
}
}
for(int j=0;j<100;j++){
out.println(count_result[j]);
}
for(int i = 0 ; i < 100 ; i++){ a[i] = i;}
int n = a.length;
for(int i = 99 ; i >= 0 ; i--)
{
int temp = (int)(Math.random()*n--);
System.out.println(a[temp]);
a[temp] = a[i]; //把该次随机到的数的位置放最后一个数
}