posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

获取随机指定限值及个数的无重复整数数组

Posted on 2006-10-20 11:54 semovy 阅读(180) 评论(0)  编辑  收藏 所属分类: JAVA基础
/*
      @param num specified the count of integer number array
      @param max specified the the max of array
*/
public int [] getRandom(int num,int max)
 {
  int onlyInt[] = new int[num],tmpInt=0,flag=0;
  for(int i=0;i<num;i++)
  {
     tmpInt =  (int)(Math.random()*max+1);
     if(i==0)
     onlyInt[0] = tmpInt;
     else
     {
       for(int j=0;j<i;j++)
       {
        if(tmpInt==onlyInt[j])
        {
         i--;//如果有重复,返回原来的一步循环
         flag=1;
         break;
        }
        else flag = 0;
       }
   }
   if(flag==0)
     onlyInt[i]=tmpInt;
   }
  return onlyInt; 
 }

只有注册用户登录后才能发表评论。


网站导航: