生成范围 (0-4) 内的随机数,并限制每个数字只能生成一定次数?
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 范围内的数字。但是,
- 一旦生成2次0,就停止生成0,
blankCount
计数0代
- 1生成4次后,停止生成1,
birdCount
算1代
- 一旦2生成1次,就停止生成2,
specialCount
算2代
- 一旦3生成2次,就停止生成3,
nullCount
算3代
- 一旦生成1次4,就停止生成4,
crashCount
算4代
我正在数组中设置一个位置作为此函数的输出。该函数位于post的末尾。
问题是让函数停止调用自身。我收到此代码的堆栈溢出错误。
目标是有两个 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;
}
首先列一个清单,包含您可以合法 return 的所有项目。
然后,洗牌。
然后,要生成一个随机数,只需从您的随机列表中使用下一项。瞧 - 现在如果你不想超过 4x a 1
,请确保你的原始列表中最多只有 4 个 1
值。
我不知道你的 random(0, 5)
是做什么的,但是用它来生成随机数听起来像是被设计破坏了(鸽子洞原理)- java 有一个随机 class有一个更好的 API 没有坏掉,一定要使用它。
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++);
}
}
注意:一旦你 'run out' 的随机数,以上将抛出 IndexOutOfBoundsException
。如果您想要其他行为,请将其编码。例如,如果您希望算法以随机顺序重新开始 (re-return 0/0/1/1/1/1/2/3/3/4, re-fresh 顺序),那么你可以这样说:
public int next() {
if (pos == list.size()) {
Collections.shuffle(list); // reshuffle
pos = 0;
}
return list.get(pos++);
}
我需要生成随机数作为名为 randomize 的函数的输出。我正在尝试生成 0 到 4 范围内的数字。但是,
- 一旦生成2次0,就停止生成0,
blankCount
计数0代 - 1生成4次后,停止生成1,
birdCount
算1代 - 一旦2生成1次,就停止生成2,
specialCount
算2代 - 一旦3生成2次,就停止生成3,
nullCount
算3代 - 一旦生成1次4,就停止生成4,
crashCount
算4代
我正在数组中设置一个位置作为此函数的输出。该函数位于post的末尾。 问题是让函数停止调用自身。我收到此代码的堆栈溢出错误。
目标是有两个 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;
}
首先列一个清单,包含您可以合法 return 的所有项目。
然后,洗牌。
然后,要生成一个随机数,只需从您的随机列表中使用下一项。瞧 - 现在如果你不想超过 4x a 1
,请确保你的原始列表中最多只有 4 个 1
值。
我不知道你的 random(0, 5)
是做什么的,但是用它来生成随机数听起来像是被设计破坏了(鸽子洞原理)- java 有一个随机 class有一个更好的 API 没有坏掉,一定要使用它。
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++);
}
}
注意:一旦你 'run out' 的随机数,以上将抛出 IndexOutOfBoundsException
。如果您想要其他行为,请将其编码。例如,如果您希望算法以随机顺序重新开始 (re-return 0/0/1/1/1/1/2/3/3/4, re-fresh 顺序),那么你可以这样说:
public int next() {
if (pos == list.size()) {
Collections.shuffle(list); // reshuffle
pos = 0;
}
return list.get(pos++);
}