java 隨機數 【指定范圍】


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

         隨機數發生器(Random)對象產生以后,通過調用不同的methodnextInt()nextLong()nextFloat()nextDouble()等獲得不同類型隨機數。

        1>生成隨機數
            Random random = new Random();
            Random random = new Random(100);//指定種子數100
            random調用不同的方法,獲得隨機數。
            如果2Random對象使用相同的種子(比如都是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]);
                   }
                }
             }
 

Java隨機數類Random介紹       
Java實用工具類庫中的類java.util.Random提供了產生各種類型隨機數的方法。它可以產生intlongfloatdouble以 及Goussian等類型的隨機數。這也是它與java.lang.Math中的方法Random()最大的不同之處,后者只產生double型的隨機 數。
Random中的方法十分簡單,它只有兩個構造方法和六個普通方法。
構造方法:
(1)public Random()
(2)public Random(long seed)
Java產生隨機數需要有一個基值seed,在第一種方法中基值缺省,則將系統時間作為seed
普通方法:
(1)public synonronized void setSeed(long seed)
該方法是設定基值seed
(2)public int nextInt()
該方法是產生一個整型隨機數。
(3)public long nextLong()
該方法是產生一個long型隨機數。
(4)public float nextFloat()
該方法是產生一個Float型隨機數。
(5)public double nextDouble()
該方法是產生一個Double型隨機數。
(6)public synchronized double nextGoussian()
該方法是產生一個double型的Goussian隨機數。
2 RandomApp.java
//import java.lang.*;
import java.util.Random;

public class RandomApp{
public static void main(String args[]){
Random ran1=new Random();
Random ran2=new Random(12345);
//創建了兩個類Random的對象。
System.out.println("The 1st set of random numbers:");
System.out.println(" Integer:"+ran1.nextInt());
System.out.println(" Long:"+ran1.nextLong());
System.out.println(" Float:"+ran1.nextFloat());
System.out.println(" Double:"+ran1.nextDouble());
System.out.println(" Gaussian:"+ran1.nextGaussian());
          //產生各種類型的隨機數
System.out.print("The 2nd set of random numbers:");
for(int i=0;i<5;i++){
System.out.println(ran2.nextInt()+" ");
if(i==2) System.out.println();
//產生同種類型的不同的隨機數。
System.out.println();
}
}
}
 


Random random=new Random();
random.nextInt();

也可以有nextFloat等等,各種基本類型都有

Math.random也可以
比如說你想要0-10之間的隨機數
你可以這樣寫
(int)(Math.random()*10);
JAVA產生指定范圍的隨機數》

JAVA產生指定范圍的隨機數》
   產生機制: 
產生Min-Max之間的數字
   實現原理:
      Math.round(Math.random()*(Max-Min)+Min)

long Temp; //不能設定為int,必須設定為long
//產生10009999的隨機數
Temp=Math.round(Math.random()*8999+1000);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM