2012-11-21 46 views
1

我正在寫一個程序,模擬1到45之間的六個數字的樂透抽籤,樣例輸出是3 7 12 27 43 28.但是我想要做的是計算相鄰數字出現的次數,例如1 4 5 29 26 41是一個肯定的答案,因爲5在4之後。Java lotto模擬

這樣做的最佳方式是什麼?

我已經試過的例子,如:

int adjacent=0; 

    for(int i =0; i<6; i++) 
    { 
     int t = test[i]+1; 
     test[i]=(int)(45*Math.random())+1; 

     if(test[i]==t) 
      adjacent++; 

     System.out.print(test[i]+" "); 
    } 

這是行不通的。

我在做什麼錯?

+1

輸出是'1 4 29 5 26 41',你仍然會計數4和5嗎? –

+0

不,他們必須在旁邊 – user1816464

+0

結果的順序是否重要?你的樂透可以重複嗎? –

回答

0

我認爲你有操作問題

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    //test[i] hasn't been set yet 
    int t = test[i]+1; 
    test[i]=(int)(45*Math.random())+1; 

    //this comparison doesn't make a whole lot of sense 
    if(test[i]==t) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 

改變它周圍的像這樣的順序:

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    test[i]=(int)(45*Math.random())+1; 
    int t = -1; 
    //Make sure this comparison only happens after the second iteration 
    //to avoid index out of bounds 
    if (i != 0) 
    { 
     //Set t to the last number + 1 instead of trying to predict the future 
     t = test[i-1] + 1; 
    } 
    //Now this comparison makes a little more sense 
    //The first iteration will compare to -1 which will always be false 
    if(test[i]==t) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 

這可以進一步簡化爲這樣:

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    test[i]=(int)(45*Math.random())+1; 

    if(i != 0 && test[i]==(test[i-1]+1)) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 
+0

所以這會給我一個積極的結果,如果是8,然後是7,或者我誤解了代碼。 – user1816464

+0

@ user1816464應該是相反的,我在縮短版本中加了一個加號,現在已經修復了。 –

+0

謝謝你現在的作品!謝謝你的所有解釋和幫助! – user1816464

0

我認爲你應該洗牌,並採取任何五。 Collections#Shuffle會幫助你,它使用默認的隨機源來排列指定的列表。所有排列發生的可能性幾乎相等。

List<Integer> list = ArrayList<Integer>(); 
list.add(1); 
list.add(2); 
Collections.shuffle(list); 
Random rnd = new Random(); 
Integer[] result = new Integer[5]; 
result[0] = list.get(rnd.getNextInt(45)); 
result[1] = list.get(rnd.getNextInt(45)); 
result[2] = list.get(rnd.getNextInt(45)); 
result[3] = list.get(rnd.getNextInt(45)); 
result[4] = list.get(rnd.getNextInt(45)); 

它總是給你隨機值,那麼你應該對它進行排序,按順序排列,比如升序排列。

Arrays.sort(result); 

現在你可以寫一個循環來找出相鄰的數字。

int adjacent = 0; 
for(int i=1; i<result.length;i++){ 
    int prev = result[i-1]; 
    int now = result[i]; 
    if(prev+1 == now) 
    adjacent++; 
} 
+0

我不確定這是否回答他的問題。但是這比使用隨機生成6個樂透號碼更好。 –

+0

問題是關於檢測相鄰值,而不是如何首先生成隨機數。 –

+0

是的,我只是想展示正確的方式來做到這一點。無論如何'現在的任何理由'? –

0

生成6個數字並將它們放入數組後。使用Arrays.sort()。然後您可以比較相鄰的數組條目。

你也應該避免使用隨機生成6個數字,因爲它可以生成重複。這可能會或可能不會準確地模擬您的樂透抽獎。 Quoi的回答對此有很好的建議。

+0

downvote的原因? –

0

你需要分開獨特的生成(因此下面的HashSet來保證身份)隨機選擇,對它們進行排序,然後確定鄰接關係:

import java.util.HashSet; 
import java.util.Arrays; 

public class Lotto 
{ 
    public Lotto() 
    { 

    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     Lotto lotto = new Lotto(); 
     lotto.randomizeSelections(5); 
    } 

    private void randomizeSelections(int numOfArrays) 
    { 
     for(int i = 0; i < numOfArrays; i++) 
     { 
      int[] selArry = new int[6]; 
      //to insure that each random selection is unique 
      HashSet<Integer> idntySet = new HashSet<Integer>(); 

      for(int j = 0; j < 6;) 
      { 
       int rndm = (int)(45 * Math.random()) + 1; 

       //add selection to the array only if it has not been randomized before 
       if(!idntySet.contains(rndm)) 
       { 
        selArry[j] = rndm; 

        j++; 
       } 

      } 

      //sort the array for determing adjacency 
      Arrays.sort(selArry); 

      for(int j = 0; j < 6; j++) 
      { 
       int sel = selArry[j]; 
       boolean isAdjcnt = (j > 0 && (sel == selArry[j - 1] + 1)) ? true : false; 

       System.out.println(i + "." + j + ".random = " + sel); 

       if(isAdjcnt) System.out.println("\tAdjacent"); 

      } 
     } 
    } 
}