【发布时间】:2023-04-07 14:01:01
【问题描述】:
您好,我正在尝试一种方法来生成一个范围内的随机数 其中可能需要一个偏差,这将使数字更有可能根据偏差而更高/更低。
目前我正在使用这个
public int randIntWeightedLow(int max, int min, int rolls){
int rValue = 100;
for (int i = 0; i < rolls ; i++) {
int rand = randInt(min, max);
if (rand < rValue ){
rValue = rand;
}
}
return rValue;
}
通过给我一个范围内的数字,这可以正常工作,我添加的卷越多,数字可能会越低。但是我遇到的问题是 3 卷和 4 卷之间存在很大差异。
我很想拥有类似的东西 公共无效randomIntWithBias(int min,int max,浮动偏差){ }
如果给出负偏差会使数字更频繁地变低,并且 正偏差会使数字更频繁地更高,但仍保持数字在最小值和最大值的随机范围内。
目前要生成我正在使用的随机数
public int randInt(final int n1, final int n2) {
if (n1 == n2) {
return n1;
}
final int min = n1 > n2 ? n2 : n1;
final int max = n1 > n2 ? n1 : n2;
return rand.nextInt(max - min + 1) + min;
}
我是 java 和一般编码的新手,所以任何帮助都将不胜感激。
【问题讨论】:
-
您可以使用
nextGaussian()乘以偏差来提供结果的偏差部分。 -
是的,但是我会用它做什么......如果我只是将它添加到随机数中,那么如果最小值和最大值将被剔除..
-
好的,请提供更多细节:准确定义结果如何受偏差参数影响,并提供一些示例。例如,偏差的大小是否会提高/降低(取决于符号)结果的下限/上限?