我正在尝试创建一个方法,其中包含一个可以创建新的 Raindrop
的循环。对象并将它们插入到列表中。每个Raindrop
对象有一个构造函数,它接受一个 int,它表示雨滴的 Z 索引。
现在的问题是我想创建更多具有较低 z 索引的水滴而不是具有较高 z 索引的水滴。
我有这个方法:
public void createRainDrops(int amount, int startIndex, endIndex) {
for (int i=0; i<=amount; i++) {
//what goes here?
dropList.add(new Drop(zIndex));
}
}
当我打电话createRainDrops(500, 10, 20);
时我期望这样的事情:
10x Drops with index 20,
20x Drops with index 19,
30x Drops with index 18,
40x Drops with index 17,
...
Most drops with index 10
或者类似的东西。我脑子里没有确切的算法,也没有准确地进行分割。与 z 指数较高相比,较低 z 指数的掉落数量更多。
最佳答案
如果您有给定的完美分布,只需将其用作权重即可。
假设有一个给定的事件,发生[i] = 索引 i 的文本中的掉落总数
那么索引 i 的项目的机会应该是occurrence[i]/sum(i 的 j element){occurrence[j] }
或者,用伪代码:
given occurence
initialize probability as array of size of occurence
compute sum s of all members of occurence
assign probability[i] = occurence[i] / sum
for every drop to create:
generate number rand as random in 0..1
initialize probsum = 0
for every index
probsum += probability[i]
if rand < probsum
create drop with the current index as argument
关于java - 迭代循环并不均匀地分配数字。越低的范围越多,越高的范围越少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220999/