溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA中如何獲取隨機數(shù)

發(fā)布時間:2021-07-29 14:43:12 來源:億速云 閱讀:154 作者:Leah 欄目:編程語言

JAVA中如何獲取隨機數(shù),相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

在Java中我們可以使用java.util.Random類來產(chǎn)生一個隨機數(shù)發(fā)生器。它有兩種形式的構(gòu)造函數(shù),分別是Random()和Random(long seed)。Random()使用當前時間即System.currentTimeMillis()作為發(fā)生器的種子,Random(long seed)使用指定的seed作為發(fā)生器的種子

[@more@]

隨機數(shù)發(fā)生器(Random)對象產(chǎn)生以后,通過調(diào)用不同的method:nextInt()、nextLong()、nextFloat()、nextDouble()等獲得不同類型隨機數(shù)。

1>生成隨機數(shù)
           Random random = new Random();
           Random random = new Random(100);//指定種子數(shù)100
           random調(diào)用不同的方法,獲得隨機數(shù)。
           如果2個Random對象使用相同的種子(比如都是100),并且以相同的順序調(diào)用相同的函數(shù),那它們返回值完全相同。如下面代碼中兩個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>指定范圍內(nèi)的隨機數(shù)
             隨機數(shù)控制在某個范圍內(nèi),使用模數(shù)運算符%
            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);
                           }
                      }
                 }
             獲得的隨機數(shù)有正有負的,用Math.abs使獲取數(shù)據(jù)范圍為非負數(shù)

3>獲取指定范圍內(nèi)的不重復隨機數(shù)
            import java.util.*;
            class TestRandom {
                  public static void main(String[] args) {
                       int[] intRet = new int[6];
                       int intRd = 0; //存放隨機數(shù)
                       int count = 0; //記錄生成的隨機數(shù)個數(shù)
                       int flag = 0; //是否已經(jīng)生成過標志
                       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]);
                  }
               }
            }

看完上述內(nèi)容,你們掌握JAVA中如何獲取隨機數(shù)的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI