1

I'm trying to create a method which contains a loop that can create new Raindrop objects and insert them into a list. Each Raindrop object has a constructor which takes an int, which represents the Z index of a rain drop.

Now the problem is that I want to create more drops with a lower z index than with a higher.

I have this method:

public void createRainDrops(int amount, int startIndex, endIndex) {

    for (int i=0; i<=amount; i++) {
        //what goes here?
        dropList.add(new Drop(zIndex));
    }
}

When I call createRainDrops(500, 10, 20); I would expect something like this:

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

Or something similar. I don't have an exact algorithm in my head, neither how the split should be done exactly. Just more drops with a lower z index, than with a higher.

Draken
  • 3,134
  • 13
  • 34
  • 54
Emil Kaminski
  • 1,886
  • 2
  • 16
  • 26

1 Answers1

0

If you have a given perfect distribution, simply use that as a weight.

Let there be a given occurrence, with occurence[i] = total amount of drops in your text for index i

Then the chance for an item of index i should be occurence[i] / sum(j element of I){ occurence[j] }

Or, in pseudo-code:

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
Aziuth
  • 3,652
  • 3
  • 18
  • 36