我正在創建一個簡單的Mastermind遊戲,其中我的顏色由0到9的數字表示。程序應生成一個長度爲3到9位的隨機代碼。我決定使用一個數組來保存我的數字0到9,但我不知道如何從這個數組中生成一個隨機數的隨機數。有人可以幫我嗎?從數組中隨機組合(Java)
回答
Random rnd=new Random()
int x=rnd.nextInt(10)
是的,我知道,但我的解釋有點愚蠢。對不起。我想從我的數組中得到一個組合。例如,如果數字是0,1,2,3,4,5,6,7,8,9,我想要隨機組合生成隨機長度。你們給我的東西有點不同。 –
所以...把上面的代碼放在一個for循環中,一次只能得到一個數字。 – jahroy
使用一個隨機數發生器來確定字符串的長度。
然後在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());
下面是示例代碼:
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();
}
太棒了。非常非常感謝你。還有一件事。我想從數組中排除零號碼。我怎樣才能做到這一點 ? –
然後在生成一個數字時寫入'rnd.nextInt(9)+ 1'而不是'rnd.nextInt(10)'。 –
好的,最後一個問題。你能告訴我怎樣才能打印數組作爲字符串,沒有任何逗號。例如嘗試#2:[8,9,6]。我希望輸入是:896.預先感謝。我希望你儘快回覆我=) –
我相信你想要的是你的數組中元素的random permutation的一部分。這可以分兩步完成。
使用隨機生成器來反覆交換數組中的元素。
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 }
選擇一個隨機長度
ln
,並採取從0
的元素ln
- 1. Java隨機數組
- 2. 隨機從數組
- 3. Java的隨機數組
- 4. 如何從所有組合中隨機選取多個組合?
- 5. Java數組隨機字母組合(ArrayList不是)
- 6. 在Java中隨機播放數組
- 7. java中的隨機數組值
- 8. 隨機數組?
- 9. 從數組中挑選隨機數
- 10. 數組中的隨機數
- 11. 最小隨機組合
- 12. 如何用隨機數從crypto.getRandomValues中隨機化數組?
- 13. Java腳本隨機數組函數
- 14. 從隨機數組中選擇值PHP
- 15. 如何隨機從數組中獲取
- 16. 從關聯數組中隨機選擇
- 17. 從數組中選擇隨機值
- 18. 從jQuery數組中隨機選擇
- 19. 從數組中讀取隨機值
- 20. 從數組中獲得隨機ID
- 21. 從數組中隨機選擇
- 22. 從數組中移除隨機值
- 23. 從數組中隨機選擇圖像
- 24. 從數組中挑選隨機顏色
- 25. 從數組中隨機選擇
- 26. 從數組列表中隨機選擇
- 27. 從數組中獲取隨機對象
- 28. 從數組中挑選隨機NSNumber?
- 29. 從數組中選擇隨機結構
- 30. 隨機數發生器的組合
你可以看到這個問題的答案[這裏] [1]。 [1]:HTTP://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – DigCamara
允許在生成的代碼包含重複的數字?例如,23389有效嗎? –