yooli88

          [轉帖]在數組里隨機取不重復的數

          方法沒有確認
          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];
                      }
                  }
              }
          }
          方法沒有確認不知道是否可行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從指定數組中取不重復的7個隨機數

          如何用java從指定數組中取不重復的7個隨機數,以下是我寫的代碼,但是是有重復的,哪位大俠賜教一下如何使用Random類的種子,幫我實現不重復的隨機數。。。
          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的世界里 閱讀(447) 評論(0)  編輯  收藏 所屬分類: 吸人精華 吐俺的糟粕 我吸(吸星大法 hoho~~~)


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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 乡城县| 南开区| 贡山| 土默特右旗| 抚顺市| 青川县| 磐安县| 临泽县| 遂川县| 长葛市| 平罗县| 静宁县| 陵水| 吐鲁番市| 遵化市| 定陶县| 宁明县| 涪陵区| 公安县| 民权县| 崇仁县| 闽侯县| 尼勒克县| 安宁市| 辽源市| 合作市| 鄢陵县| 朝阳市| 苍溪县| 大石桥市| 开江县| 徐汇区| 栾城县| 永春县| 澄迈县| 玛沁县| 谢通门县| 行唐县| 肃宁县| 洮南市| 通化县|