英文:
Generating random numbers in a range (0-4) with the constraint that each number can only be generated a certain amount of times?
问题
我需要为名为"randomize"的函数生成随机数作为输出。我正在尝试在0到4的范围内生成数字。但是,
- 一旦生成了0两次,停止生成0,
blankCount
计数0的生成次数 - 一旦生成了1四次,停止生成1,
birdCount
计数1的生成次数 - 一旦生成了2一次,停止生成2,
specialCount
计数2的生成次数 - 一旦生成了3两次,停止生成3,
nullCount
计数3的生成次数 - 一旦生成了4一次,停止生成4,
crashCount
计数4的生成次数
我正在将数组中的一个位置设置为此函数的输出。该函数位于帖子末尾。
问题是如何使函数停止调用自身。我在这段代码中遇到了堆栈溢出错误。
目标是得到两个0,四个1,一个2,两个3和一个4
0,0,1,1,1,1,2,3,3,4
有人能帮忙让这个函数正常工作吗?
int blankCount = 0;
int birdCount = 0;
int specialCount = 0;
int nullCount = 0;
int crashCount = 0;
int randomize(){
int num = 0;
int randomNum = floor(random(0,5));
if(randomNum == 0 && blankCount <= 1) {
blankCount++;
num = randomNum;
}else if(randomNum == 0 && blankCount == 2){
num = randomize();
}else if(randomNum == 1 && birdCount <= 3){
birdCount++;
num = randomNum;
}else if(randomNum == 1 && birdCount == 4){
num = randomize();
}else if(randomNum == 2 && specialCount <= 0){
specialCount++;
num = randomNum;
}else if(randomNum == 2 && specialCount == 1){
num = randomize();
}else if(randomNum == 3 && nullCount <= 1){
nullCount++;
num = randomNum;
}else if(randomNum == 3 && nullCount == 2){
num = randomize();
}else if(randomNum == 4 && crashCount <= 0){
crashCount++;
num = randomNum;
}else if(randomNum == 4 && crashCount == 1){
num = randomize();
}else{
print("H ");
}
return num;
}
英文:
I need to generate random numbers as an output for a function called randomize. I am trying to generate numbers in the range 0 to 4. However,
- Once 0 is generated 2 times, stop generating 0,
blankCount
counts 0 generations - Once 1 is generates 4 times, stop generating 1,
birdCount
counts 1 generations - Once 2 is generated 1 time, stop generating 2,
specialCount
counts 2 generations - Once 3 is generated 2 times, stop generating 3,
nullCount
counts 3 generations - Once 4 is generated 1 time, stop generating 4,
crashCount
counts 4 generations
I am setting a place in an array to the output of this function. The function is at the end of the post.
The problem is to get the function to stop calling itself. I get a Stack Overflow Error with this code.
The goal is to have Two 0s, Four 1s, One 2, Two 3s, and One 4
>0,0,1,1,1,1,2,3,3,4
Can anyone help make this function work?
int blankCount = 0;
int birdCount = 0;
int specialCount = 0;
int nullCount = 0;
int crashCount = 0;
int randomize(){
int num = 0;
int randomNum = floor(random(0,5));
if(randomNum == 0 && blankCount <= 1) {
blankCount++;
num = randomNum;
}else if(randomNum == 0 && blankCount == 2){
num = randomize();
}else if(randomNum == 1 && birdCount <= 3){
birdCount++;
num = randomNum;
}else if(randomNum == 1 && birdCount == 4){
num = randomize();
}else if(randomNum == 2 && specialCount <= 0){
specialCount++;
num = randomNum;
}else if(randomNum == 2 && specialCount == 1){
num = randomize();
}else if(randomNum == 3 && nullCount <= 1){
nullCount++;
num = randomNum;
}else if(randomNum == 3 && nullCount == 2){
num = randomize();
}else if(randomNum == 4 && crashCount <= 0){
crashCount++;
num = randomNum;
}else if(randomNum == 4 && crashCount == 1){
num = randomize();
}else{
print("H ");
}
return num;
}
答案1
得分: 2
首先,创建一个包含所有可以合法返回的项目的列表。
然后,对列表进行洗牌。
接下来,要生成一个随机数,只需从洗牌后的列表中获取下一个项目。哇 - 现在,如果您希望1
的数量不超过4次,只需确保原始列表中最多有4个1
值。
我不知道你的random(0, 5)
是什么意思,但使用它来生成随机数听起来设计上是有问题的(鸽巢原理)- Java有一个更好的API的Random类,不会出现这种问题,务必使用它。
class Randomizer {
private static final List<Integer> INPUTS = List.of(0, 0, 1, 1, 1, 1, 2, 3, 3, 4);
private final List<Integer> list;
private int pos = 0;
public Randomizer() {
list = new ArrayList<Integer>(INPUTS);
Collections.shuffle(list);
}
public int next() {
return list.get(pos++);
}
}
注意:一旦你用尽了随机数,上述代码将抛出IndexOutOfBoundsException
。如果你希望有其他行为,在代码中实现。例如,如果你希望算法重新开始(以随机顺序重新返回0/0/1/1/1/1/2/3/3/4,刷新顺序),你可以这样做:
public int next() {
if (pos == list.size()) {
Collections.shuffle(list); // 重新洗牌
pos = 0;
}
return list.get(pos++);
}
英文:
First make a list containing every item that you can legally return.
Then, shuffle it.
Then, to generate a random number, just consume the next item from your shuffled list. Voila - now if you never want more than 4x a 1
, make sure only at most 4 1
values are in your original list.
I have no idea what your random(0, 5)
does, but using that to generate random numbers sounds like it's broken by design (pigeon hole principle) - java has a Random class with a much nicer API that isn't broken, by all means use that.
class Randomizer {
private static final List<Integer> INPUTS = List.of(0, 0, 1, 1, 1, 1, 2, 3, 3, 4);
private final List<Integer> list;
private int pos = 0;
public Randomizer() {
list = new ArrayList<Integer>(INPUTS);
Collections.shuffle(list);
}
public int next() {
return list.get(pos++);
}
}
NB: The above will throw IndexOutOfBoundsException
once you 'run out' of random numbers. If you want other behaviour, code that in. For example, if you want the algorithm to start over (re-return 0/0/1/1/1/1/2/3/3/4 in random order, re-fresh the order), then you could do, say:
public int next() {
if (pos == list.size()) {
Collections.shuffle(list); // reshuffle
pos = 0;
}
return list.get(pos++);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论