yooli88

          [轉(zhuǎn)帖]在數(shù)組里隨機(jī)取不重復(fù)的數(shù)

          方法沒有確認(rèn)
          import java.util.*;
          public class Lottery {

              public static void main(String[] args) {
                  int[] data1 = {3, 5, 6, 8, 9, 15, 18, 24, 27, 30, 32};
                  Random r = new Random();
                  int irdm = 0;
                  for(int i = 0; i < 7; i ++) {
                      irdm = r.nextInt(11 - i);
                      System.out.println(data1[irdm]);
                      for(int j = irdm; j < 11 - i - 1; j ++) {
                          data1[j] = data1[j + 1];
                      }
                  }
              }
          }
          方法沒有確認(rèn)不知道是否可行public class AAAAA {
                  public static void main(String[] args) {

                          int i = 0;
                          int j = 0;
                          int[] temp = new int[20];
                          for (j = 0; j < temp.length; j++) {
                                  temp[j] = Math.random() * 100  + 1;
                                  System.out.print(temp[j] + ",";
                          }
                          
                          HashSet hh = new HashSet();
                          while (hh.size() < 7) {
                                  int aa = (int) (Math.random() * 100 + 1);
                                  hh.add(aa);
                          }
                          System.out.println(hh.size());
                          Iterator ii = hh.iterator();
                          while (ii.hasNext()) {
                                  System.out.print(ii.next() + ",";
                          }
                  }

          }

          java從指定數(shù)組中取不重復(fù)的7個隨機(jī)數(shù)

          如何用java從指定數(shù)組中取不重復(fù)的7個隨機(jī)數(shù),以下是我寫的代碼,但是是有重復(fù)的,哪位大俠賜教一下如何使用Random類的種子,幫我實現(xiàn)不重復(fù)的隨機(jī)數(shù)。。。
          import java.util.*;
          public class Lottery {

              public static void main(String[] args)
              {
                  int[] data1 = {3,5,6,8,9,15,18,24,27,30,32};
                  Random   r=new   Random();
                  StringBuffer   str1=new   StringBuffer();
                  for(int   i=0;i<6;i++)
                  {
                  str1.append(data1[r.nextInt(11)]);
                   }
                   System.out.println(str1);
             }
          }

          posted on 2011-01-31 16:57 迷茫在java的世界里 閱讀(451) 評論(0)  編輯  收藏 所屬分類: 吸人精華 吐俺的糟粕 我吸(吸星大法 hoho~~~)


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 漳州市| 驻马店市| 叶城县| 睢宁县| 浙江省| 河曲县| 格尔木市| 沂南县| 固阳县| 汝阳县| 荔浦县| 建湖县| 邹平县| 肥城市| 济源市| 宜州市| 万州区| 五指山市| 甘孜县| 罗山县| 长乐市| 仁化县| 佳木斯市| 渝北区| 修水县| 文水县| 泾川县| 渑池县| 呼和浩特市| 沁源县| 合山市| 康保县| 郸城县| 武威市| 乌兰察布市| 兰州市| 宜兰市| 静乐县| 巴彦县| 兴化市| 阜阳市|