2013-01-23 64 views
0

我正在創建一個簡單的Mastermind遊戲,其中我的顏色由0到9的數字表示。程序應生成一個長度爲3到9位的隨機代碼。我決定使用一個數組來保存我的數字0到9,但我不知道如何從這個數組中生成一個隨機數的隨機數。有人可以幫我嗎?從數組中隨機組合(Java)

+0

你可以看到這個問題的答案[這裏] [1]。 [1]:HTTP://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – DigCamara

+0

允許在生成的代碼包含重複的數字?例如,23389有效嗎? –

回答

1

使用random number generator

Random rnd=new Random() 
int x=rnd.nextInt(10) 
+0

是的,我知道,但我的解釋有點愚蠢。對不起。我想從我的數組中得到一個組合。例如,如果數字是0,1,2,3,4,5,6,7,8,9,我想要隨機組合生成隨機長度。你們給我的東西有點不同。 –

+2

所以...把上面的代碼放在一個for循環中,一次只能得到一個數字。 – jahroy

0

使用一個隨機數發生器來確定字符串的長度。

然後在for循環中使用一個隨機數發生器來選擇數組中的每個數字。

事情是這樣的:

// the array of valid numbers 
int[] numbers = // intialize this however you want 

Random rand = new Random(); 

// determine random length between 3 and 9 
int numLength = rand.nextInt(7) + 3; 

StringBuilder output = new StringBuilder(); 

for (int i = 0; i < numLength; i++) { 
    // use a random index to choose a number from array 
    int thisNum = rand.nextInt(numbers.length); 
    // append random number from array to output 
    output.append(thisNum); 
} 

System.out.println(output.toString()); 
1

下面是示例代碼:

package testing.Tests_SO; 

import java.util.Arrays; 
import java.util.Random; 

public class App14487237 { 

    // creating random number generator object 
    public static final Random rnd = new Random(); 

    public static int[] getNumber() { 

     // generating array length as rundom from 3 to 9 inclusive 
     int length = rnd.nextInt(7) + 3; 

     // creating an array 
     int[] number = new int[length]; 

     // filling an array with random numbers from 0 to 9 inclusive 
     for(int i=0; i<number.length; ++i) { 
      number[i] = rnd.nextInt(10); 
     } 

     // returning and array 
     return number; 

    } 


    public static void main(String[] args) { 

     // generating number 10 times and prin result 
     for(int i=0; i<10; ++i) { 
      System.out.println("attempt #" + i + ": " + Arrays.toString(getNumber() )); 
     } 
    } 
} 

下面是它的輸出:

attempt #0: [0, 2, 6, 2, 5, 7, 5, 3] 
attempt #1: [6, 2, 6, 6, 6, 2] 
attempt #2: [8, 9, 6] 
attempt #3: [6, 4, 7, 2, 1, 5, 7, 0] 
attempt #4: [8, 2, 6, 7, 3, 8, 2, 9, 1] 
attempt #5: [8, 6, 5, 9, 8, 8, 3, 9] 
attempt #6: [6, 2, 3, 8, 6] 
attempt #7: [3, 4, 6, 2] 
attempt #8: [0, 5, 0, 0, 5, 8, 9, 4, 6] 
attempt #9: [2, 2, 3, 3, 4, 9, 0] 

附:

打印關閉:

int[] number; 
for(int i=0; i<10; ++i) { 
    number = getNumber(); 
    System.out.print("attempt #" + i + ": "); 
    for(int j=0; j<number.length; ++j) { 
     System.out.print(number[j]); 
    } 
    System.out.println(); 
} 
+0

太棒了。非常非常感謝你。還有一件事。我想從數組中排除零號碼。我怎樣才能做到這一點 ? –

+0

然後在生成一個數字時寫入'rnd.nextInt(9)+ 1'而不是'rnd.nextInt(10)'。 –

+0

好的,最後一個問題。你能告訴我怎樣才能打印數組作爲字符串,沒有任何逗號。例如嘗試#2:[8,9,6]。我希望輸入是:896.預先感謝。我希望你儘快回覆我=) –

0

我相信你想要的是你的數組中元素的random permutation的一部分。這可以分兩步完成。

  1. 使用隨機生成器來反覆交換數組中的元素。

    int n = arr.length; 
    for (int i = 0; i < n-1; i++) { 
        int j = i + rnd.nextInt(n-i); // select element between i and n 
        // swap elements i and j in the array 
    } 
    
  2. 選擇一個隨機長度ln,並採取從0的元素ln