posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          獲取隨機指定限值及個數的無重復整數數組

          Posted on 2006-10-20 11:54 semovy 閱讀(188) 評論(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;?
          ?}
          主站蜘蛛池模板: 高要市| 库车县| 长兴县| 嘉鱼县| 眉山市| 浙江省| 个旧市| 如东县| 炉霍县| 丁青县| 河津市| 寻乌县| 普兰县| 昌都县| 仁布县| 九寨沟县| 孟连| 湟源县| 和静县| 彰化市| 法库县| 霍州市| 剑河县| 英山县| 武平县| 繁峙县| 屯昌县| 茌平县| 神池县| 新郑市| 康马县| 连江县| 北碚区| 抚宁县| 肥西县| 莱州市| 永定县| 周宁县| 孝感市| 洞口县| 中山市|