迭代循环并不均匀地分布数字.在较低的范围内,更高的范围更少



我正在尝试创建一个包含一个可以创建新Raindrop对象并将它们插入列表的循环的方法。每个 Raindrop对象都有一个构造函数,该构造函数采用一个int,代表降雨的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索引相比,降低的滴度要多。

如果您具有给定的完美分布,只需将其用作重量。

让给定的出现,出现[i] =索引i

的文本中的总数

然后,索引i的机会应该是出现[i]/sum(i(

或在伪代码中:

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