如何使用Java生成1到6之间的6个随机数?
我在Java中遇到了一个生成6个介于1和6之间的随机数的问题。所有数字都必须是唯一的。当我输入kolon值5时,数组应该如下所示:如何使用Java生成1到6之间的6个随机数?,java,arrays,random,numbers,prng,Java,Arrays,Random,Numbers,Prng,我在Java中遇到了一个生成6个介于1和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 我不希望程序生成相同的两个数字。这里怎么了 相关代码: public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
我不希望程序生成相同的两个数字。这里怎么了
相关代码:
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();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入行数量:”);
int kolon=input.nextInt();
整数[][]dizi_asil=新整数[kolon][6];
对于(int i=0;i
这是一种简单的方法:
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.
List List=Arrays.asList(1,2,3,4,5,6);
集合。洗牌(列表);
//如果需要,可以将其转换为数组。
创建一个包含1到6的列表。然后使用将其洗牌。然后您将得到随机唯一数试试看
List List=new ArrayList();
对于(inti=1;i一个非常简单的修复-将u++;
替换为u-->
++
将使循环停止,-->
将使循环继续
虽然我会建议一些更像下面的东西。我希望它足够容易理解
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];
}
Integer[]dizi=新整数[6];
对于(int j=0;j<6;j++)
{
布尔值是有效的;
做
{
dizi[j]=(int)((Math.random()*6)+1);
isValid=true;
对于(int k=0;isValid&&k
我还建议,它有一个nextInt(int)
方法,比(int)((Math.random()*6)+1)
更好
但洗牌可能是一种更快的方法。要么像其他答案一样使用API,要么研究简单的洗牌算法。一种更短的方法是使用许多人已经提到的洗牌
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);
}
}
进口:
import acm.util.RandomGenerator;
private RandomGenerator rgen = RandomGenerator.getInstance();
实际采摘:
randnum = rgen.nextInt(1, 6);
这会选择一个介于0和5之间的随机数。您可以对1到6执行此操作:
randnum = rgen.nextInt(2, 7);
或:
6个介于1和6之间的唯一数字不是随机的。它们将始终是1,2,3,4,5,6。它们的顺序可能是随机的,但不是数字。在您的示例中,如果kolon=5,为什么会有4行?此外,您正在调用Arrays.sort(dizi_asil[i])
。那个么你们为什么期望你们的数组不会被排序呢???例如:当我运行程序时,有时我会得到1 2 4 5 6。我必须总是得到1 2 3 4 5 6,这是错误的。你们不是想生成6个随机数,而是一个随机排列的数字1到6。试着像其他人在t中指出的那样收集。洗牌当我运行程序时,有时我会得到1 2 4 5 6 6。我总是得到1 2 3 4 5是错误的6@user2204772:然后停止使用低效(和有缺陷)的方法,使用我提供的代码段。
import acm.util.RandomGenerator;
private RandomGenerator rgen = RandomGenerator.getInstance();
randnum = rgen.nextInt(1, 6);
randnum = rgen.nextInt(2, 7);
randnum = rgen.nextInt(1, 6);
randnum++;