java產生隨機數的幾種方式

一.在j2se裏咱們可使用Math.random()方法來產生一個隨機數,這個產生的隨機數是0-1之間的一個double,咱們能夠把他乘以必定的數,好比說乘以100,他就是個100之內的隨機,這個在j2me中沒有。 

二.在java.util這個包裏面提供了一個Random的類,咱們能夠新建一個Random的對象來產生隨機數,他能夠產生隨機整數、隨機float、隨機double,隨機long,這個也是咱們在j2me的程序裏常常用的一個取隨機數的方法。 

三.在咱們的System類中有一個currentTimeMillis()方法,這個方法返回一個從1970年1月1號0點0分0秒到目前的一個毫秒數,返回類型是long,咱們能夠拿他做爲一個隨機數,咱們能夠拿他對一些數取模,就能夠把他限制在一個範圍以內啦 

其實在Random的默認構造方法裏也是使用上面第三種方法進行隨機數的產生的 


對於方法二中的Random類有如下說明: 

java.util.Random類有兩種方式構建方式:帶種子和不帶種子 

不帶種子: 
此種方式將會返回隨機的數字,每次運行結果不同 

public class RandomTest { 
public static void main(String[] args) { 
  java.util.Random r=new java.util.Random(); 
for(int i=0;i<10;i++){ 
    System.out.println(r.nextInt()); 



帶種子: 
此種方式,不管程序運行多少次,返回結果都是同樣的 

public static void main(String[] args) { 
  java.util.Random r=new java.util.Random(10); 
  for(int i=0;i<10;i++){ 
    System.out.println(r.nextInt()); 
  } 


兩種方式的差異在於 

(1) 首先請打開Java Doc,咱們會看到Random類的說明: 

此類的實例用於生成僞隨機數流,此類使用 48 位的種子,該種子可使用線性同餘公式對其進行修改(請參閱 Donald Knuth 的《The Art of Computer Programming, Volume 2》,第 3.2.1 節)。  

若是用相同的種子建立兩個 Random 實例,則對每一個實例進行相同的方法調用序列,它們將生成並返回相同的數字序列。爲了保證明現這種特性,咱們爲類Random指定了特定的算法。爲了 Java 代碼的徹底可移植性,Java 實現必須讓類 Random 使用此處所示的全部算法。可是容許 Random 類的子類使用其餘算法,只要其符合全部方法的常規協定便可。  

Java Doc對Random類已經解釋得很是明白,咱們的測試也驗證了這一點。 

(2) 若是沒有提供種子數,Random實例的種子數將是當前時間的毫秒數,能夠經過System.currentTimeMillis()來得到當前時間的毫秒數。打開JDK的源代碼,咱們能夠很是明確地看到這一點。 

/**  
* Creates a new random number generator. Its seed is initialized to  
* a value based on the current time: 
* Random() { this(System.currentTimeMillis()); }java.lang.System#currentTimeMillis() 
*/ 
public Random() { this(System.currentTimeMillis()); } 


另外:  

random對象的nextInt(),nextInt(int n)方法的說明: 

int nextInt()  
    返回下一個僞隨機數,它是此隨機數生成器的序列中均勻分佈的 int 值。  
int nextInt(int n)  
    返回一個僞隨機數,它是今後隨機數生成器的序列中取出的、在 0(包括)和指定值(不包括)之間均勻分佈的 int值。 html

 

轉自: http://www.blogjava.net/cool2009/archive/2009/03/15/259882.htmljava

 

  1. 關於nextInt()函數的一點兒說明:  
  2. 若是想獲得30到200的(包含30和200)這個跨度的數在java中通常能夠有以下方式得到  
  3. 1)int i = (int)(Math.random()*171) + 30;  
  4.   
  5. 2)Random r = new Random () ;  
  6.      r.nextInt (201) ;     // 這個是0 - 200  
  7.   
  8. 3)Random r = new Random () ;  
  9.      r.nextInt (171) + 30 ; // 這個是30 到 200.  
  10.   
  11. //以下爲二維數組的一點兒東西  
  12. public class 數組的使用說明代碼 {  
  13.      public static void main(String args[]){  
  14.          int[] array=creatArray(10);   
  15.             printArray(array);   
  16.      }  
  17.      public static int[] creatArray(int length){        //構造含length個元素的數組的方法  
  18.           int[] array =new int[length];   
  19.           Random rad=new Random();                //產生隨機數的方法(系統本身的)  
  20.           for(int i=0;i<array.length;i++){   
  21.               int value = rad.nextInt(100) + 200;       //rad.nextInt(100) 意思是隨機產生一個大於等於0小於100的數  ------即包含0不包含100  
  22.               array[i]=value;   
  23.            }   
  24.          return array;   
  25.       }   
  26.        
  27.      public static void printArray(int[] array){   
  28.         for(int i=0;i<array.length;i++)   
  29.             System.out.println(array[i]+'\t');   
  30.           }   
  31. }  

 http://zhangit.iteye.com/blog/1706152git

 

糾正你一點小錯誤
帶種子:  此種方式,不管程序運行多少次,返回結果都是同樣的  public static void main(String[] args) {  java.util.Random r=new java.util.Random(10);  for(int i=0;i<10;i++){  System.out.println(r.nextInt());  }  }  這個地方其實產生的隨機數是不同的 ;應該這樣寫 public static void main(String[] args) {  java.util.Random r=new java.util.Random(10);  for(int i=0;i<10;i++){  System.out.println(r.nextInt());  }  java.util.Random r2=new java.util.Random(10);  for(int i=0;i<10;i++){  System.out.println(r2.nextInt());  }  }  這樣寫的代碼隨機數產生的是同樣的;結果以下 -1157793070 1913984760 1107254586 1773446580 254270492 -1408064384 1048475594 1581279777 -778209333 1532292428 -1157793070 1913984760 1107254586 1773446580 254270492 -1408064384 1048475594 1581279777 -778209333 1532292428
相關文章
相關標籤/搜索