[英]How to generate specified amount of random numbers?
Okay, I have a stack sized 24. I need to fill it with random numbers between 1-6.好的,我有一个大小为 24 的堆栈。我需要用 1-6 之间的随机数填充它。 But the condition is every numbers amount should be the same.
但条件是每个数字数量都应该相同。 Like four times 1, four times 2, four times 3. At the end of the day I need to have stack sized 24 that contains random numbers between 1-6 but every numbers amount should be 4. I need to use only stack, I can't use arrays or array list.
就像四次 1、四次 2、四次 3。在一天结束时,我需要堆栈大小为 24,其中包含 1-6 之间的随机数,但每个数字的数量应该是 4。我只需要使用堆栈,我不能使用数组或数组列表。 How can I do it?
我该怎么做?
int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
while(!(ottf.isFull())) {
int x = rnd.nextInt(6) + 1;
if (x == 1)
counter1++;
else if (x == 2)
counter2++;
else if (x == 3)
counter3++;
else if (x == 4)
counter4++;
else if (x == 5)
counter5++;
else
counter6++;
if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)
ottf.push(x);}
I've tried to do it with counters but i guess this won't fix my problem.我试过用计数器来做,但我想这不会解决我的问题。
if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)
I guess this is wrong, because you use &&
which stops any number of being added if one of the counters are over 4.我猜这是错误的,因为您使用
&&
会在计数器之一超过 4 时停止添加任意数量。
Use this instead:改用这个:
if (x == 1 && counter1 < 4)
ottf.push(x);
counter1++;
etc. Try debugging and post your result, if you have issues等。如果有问题,请尝试调试并发布结果
Can you try this?你能试试这个吗? I gave you the piece of code regarding your idea.
我给了你一段关于你的想法的代码。 it's shorter and cleaner.
它更短更干净。
//this array is for counting only. The output is also a stack.
int[] count = new int[6];
while(!ottf.isFull()) {
int x = rnd.nextInt(6) + 1;
if(count[x-1]>4) {
continue;
}
count[x-1]++;
if (count[x-1]<=4) {
//push to stack
ottf.push(x);
}
}
Try this.尝试这个。 It only uses Stack.
它只使用堆栈。 No arrays and no lists.
没有数组和列表。
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < 24; i++) {
stack.push((i%6)+1);
}
Collections.shuffle(stack);
System.out.println(stack);
The output will always be different unless you seed shuffle.除非您进行随机播放,否则输出将始终不同。 But here is one example.
但这是一个例子。
[5, 2, 1, 6, 3, 4, 5, 1, 4, 3, 2, 6, 1, 4, 3, 6, 2, 5, 3, 2, 6, 5, 4, 1]
Such cases can be handled using switch...case
in a better way.这种情况可以使用
switch...case
更好地处理。
Do it as follows:请按以下步骤操作:
import java.util.Collections;
import java.util.Random;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Random rnd = new Random();
int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
Stack ottf = new Stack();
do {
int x = rnd.nextInt(6) + 1;
switch (x) {
case 1:
if (counter1 < 4) {
ottf.push(x);
counter1++;
}
break;
case 2:
if (counter2 < 4) {
ottf.push(x);
counter2++;
}
break;
case 3:
if (counter3 < 4) {
ottf.push(x);
counter3++;
}
break;
case 4:
if (counter4 < 4) {
ottf.push(x);
counter4++;
}
break;
case 5:
if (counter5 < 4) {
ottf.push(x);
counter5++;
}
break;
case 6:
if (counter6 < 4) {
ottf.push(x);
counter6++;
}
}
} while (ottf.size() < 24);
Collections.sort(ottf);// Not required. Doing it just to validate the result quickly
System.out.println(ottf);
}
}
Output:输出:
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.