如何使用 Java 生成 1 到 6 之间的 6 个随机数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16174056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I generate 6 random numbers between 1 and 6 using Java?
提问by Ertu?rul ?etin
I am encountering a problem generating 6 random numbers between 1 and 6 in Java. All the numbers have to be unique. When I enter kolon value 5, the arrays should be like this:
我遇到了在 Java 中生成 1 到 6 之间的 6 个随机数的问题。所有的数字都必须是唯一的。当我输入 kolon 值 5 时,数组应该是这样的:
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
I don't want the program to generate the same two numbers. What is wrong here?
我不希望程序生成相同的两个数字。这里有什么问题?
Relevant code:
相关代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter row quantity: ");
int kolon = input.nextInt();
Integer[][] dizi_asil = new Integer[kolon][6];
for (int i = 0; i < kolon; i++) {
Integer[] dizi = new Integer[6];
for (int j = 0; j < 6; j++) {
dizi[j] = (int) ((Math.random() * 6) + 1);
for (int u = 0; u < 1; u++) {
for (int k = 0; k < j; k++) {
while (dizi[k] == dizi[j]) {
dizi[j] = (int) ((Math.random()* 6) + 1);
u++;
}
}
}
dizi_asil[i][j] = dizi[j];
}
Arrays.sort(dizi_asil[i]);
}
for (int i = 0; i < dizi_asil.length; i++) {
for (int k = 0; k < dizi_asil[i].length; k++) {
System.out.print(dizi_asil[i][k] + "\t");
}
System.out.println();
}
回答by stinepike
create a list containing 1 to 6. then shuffle it using Collection.shuffle. Then you will get random unique number
创建一个包含 1 到 6 的列表。然后使用Collection.shuffle对其进行洗牌。然后你会得到随机的唯一号码
回答by jlordo
This is an easy way:
这是一个简单的方法:
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
Collections.shuffle(list);
// you can convert it to an array if you need to.
回答by Bernhard Barker
A very simple fix - replace u++;
with u--;
. ++
will make the loop stop, --
will make it carry on.
一个非常简单的修复 - 替换u++;
为u--;
. ++
将使循环停止,--
将使其继续。
Though I'd suggest something more like the below. I hope it's easy enough to understand.
虽然我建议更像下面的东西。我希望它很容易理解。
Integer[] dizi = new Integer[6];
for (int j = 0; j < 6; j++)
{
boolean isValid;
do
{
dizi[j] = (int) ((Math.random() * 6) + 1);
isValid = true;
for (int k = 0; isValid && k < j; k++)
if (dizi[k] == dizi[j])
isValid = false;
}
while (!isValid);
dizi_asil[i][j] = dizi[j];
}
I'd also suggest the Random class, which has a nextInt(int)
method, which is better than (int) ((Math.random() * 6) + 1)
.
我还建议Random 类,它有一个nextInt(int)
方法,比(int) ((Math.random() * 6) + 1)
.
But shuffling is probably a faster way to do it. Either use the API like one of the other answers or look into the Fisher-Yates / Knuth shufflefor an easy shuffle algorithm.
但是改组可能是一种更快的方法。要么像其他答案一样使用 API,要么查看Fisher-Yates / Knuth shuffle以获得简单的 shuffle 算法。
回答by Achintya Jha
Try Collections.shuffle(list);
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i<7 ; i++)
list.add(i);
Or you can do :
或者你可以这样做:
List<Integer> list = Arrays.asList(1,2,3,4,5,6)
Collections.shuffle(list);
Iterate the list and get unique value each time.
迭代列表并每次获得唯一值。
回答by Peter Lawrey
A much shorter way is to use shuffle as many have mentioned already.
正如许多人已经提到的那样,一种更短的方法是使用 shuffle。
public static void main(String... ignored) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter row quantity: ");
List<Integer> rolls = Arrays.asList(1, 2, 3, 4, 5, 6);
for (int i = 0, rows = input.nextInt(); i < rows; i++) {
Collections.shuffle(rolls);
System.out.println(rolls);
}
}
if you run it you get something like
如果你运行它,你会得到类似的东西
Please enter row quantity:
5
[5, 6, 3, 2, 4, 1]
[5, 4, 3, 6, 2, 1]
[6, 4, 2, 3, 5, 1]
[3, 1, 6, 2, 5, 4]
[4, 1, 6, 3, 5, 2]
回答by A.J. Uppal
Imports:
进口:
import acm.util.RandomGenerator;
private RandomGenerator rgen = RandomGenerator.getInstance();
Actually picking:
实际采摘:
randnum = rgen.nextInt(1, 6);
This picks a random number between 0 and 5. You can just do this for 1 through 6:
这会在 0 到 5 之间选择一个随机数。您可以只对 1 到 6 执行此操作:
randnum = rgen.nextInt(2, 7);
Or:
或者:
randnum = rgen.nextInt(1, 6);
randnum++;