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的世界里 閱讀(451) 評論(0)  編輯  收藏 所屬分類: 吸人精華 吐俺的糟粕 我吸(吸星大法 hoho~~~)


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


          網站導航:
           
          主站蜘蛛池模板: 东兰县| 凤冈县| 巴南区| 玉树县| 东宁县| 固始县| 武威市| 平和县| 玉环县| 丰镇市| 黎川县| 汾西县| 白水县| 岢岚县| 夏邑县| 辰溪县| 洛南县| 武邑县| 延庆县| 高雄市| 彭阳县| 乌苏市| 哈密市| 安康市| 剑阁县| 鄱阳县| 兴城市| 青海省| 泸州市| 铜陵市| 吉木萨尔县| 龙川县| 扎兰屯市| 台江县| 武陟县| 张掖市| 临安市| 保康县| 寻乌县| 普安县| 六枝特区|