java隨機數

來源:互聯網
上載者:User

java.util.Random類來產生一個隨機數發生器。它有兩種形式的建構函式,分別是Random()和Random(long seed)。Random()使用目前時間即System.currentTimeMillis()作為發生器的種子,Random(long seed)使用指定的seed作為發生器的種子。
        隨機數發生器(Random)對象產生以後,通過調用不同的method:nextInt()、nextLong()、nextFloat()、nextDouble()等獲得不同類型隨機數。

       1>產生隨機數
           Random random = new Random();
           Random random = new Random(100);//指定種子數100
           random調用不同的方法,獲得隨機數。
           如果2個Random對象使用相同的種子(比如都是100),並且以相同的順序調用相同的函數,那它們傳回值完全相同。如下面代碼中兩個Random對象的輸出完全相同
          import java.util.*;
          class TestRandom {
                public static void main(String[] args) {
                     Random random1 = new Random(100);
                     System.out.println(random1.nextInt());
                     System.out.println(random1.nextFloat());
                     System.out.println(random1.nextBoolean());
                     Random random2 = new Random(100);
                     System.out.println(random2.nextInt());
                     System.out.println(random2.nextFloat());
                     System.out.println(random2.nextBoolean());
                }
            }

        2>指定範圍內的隨機數
             隨機數控制在某個範圍內,使用模數運算子%
            import java.util.*;
                 class TestRandom {
                      public static void main(String[] args) {
                           Random random = new Random();
                           for(int i = 0; i < 10;i++) {
                               System.out.println(Math.abs(random.nextInt())%10);
                           }
                      }
                 }
             獲得的隨機數有正有負的,用Math.abs使擷取資料範圍為非負數

       3>擷取指定範圍內的不重複隨機數
            import java.util.*;
            class TestRandom {
                  public static void main(String[] args) {
                       int[] intRet = new int[6];
                       int intRd = 0; //存放隨機數
                       int count = 0; //記錄產生的隨機數個數
                       int flag = 0; //是否已經產生過標誌
                       while(count<6){
                            Random rdm = new Random(System.currentTimeMillis());
                            intRd = Math.abs(rdm.nextInt())%32+1;
                            for(int i=0;i<count;i++){
                                if(intRet[i]==intRd){
                                    flag = 1;
                                    break;
                                }else{
                                    flag = 0;
                                }
                            }
                            if(flag==0){
                                intRet[count] = intRd;
                                count++;
                            }
                   }
                  for(int t=0;t<6;t++){
                      System.out.println(t+"->"+intRet[t]);
                  }
               }
            }

詳細請看文檔

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.