I had a few minutes today, so I thought perhaps it was a chance to think about pumpkins.

I added pumpkins as a placeholder originally, so that my resource type had more than just herbs. But it would be nice if I introduce them as a rare resource. You’re not going to find them often when foraging in a forest, but occasionally seems plausible. So I decided that I’d make them occasionally appear when foraging – and that means its time for a post about maths and balancing.

### Simple version – average of one per hour

Firstly how many do I want? It seems like about 1 per hour is reasonable. Enough that you see them occasionally but not enough that you have tons of them.

Next, how many chances do I have in that hour? Every hour has 60 minutes * 60 seconds * 5 cycles per second = 18000 cycles per hour; but forage takes 20 cycles. So we have 900 forage actions per hour.

But each forage gives you 20 items, so we’re back to 18000 foraged items per hour.

To get one per hour, then we’re talking simple division: 18000 cycles per hour, so 1/18000 chance per cycle, and we’ll get approximately one per hour.

This doesn’t mean we’ll see one every hour. Some more maths: the chance of us not getting any pumpkin is 17999/18000 on a given forage roll. If we didn’t get any at all, we’d not get any pumpkins in all 18000 rolls, so we have to multiple them all together 18000 times – so we have (17999/18000) ^ 18000 = 0.368 (approx). This means that 37% of the time we won’t see one in an hour. Perhaps that’s too rare?

### Complex version – usually get at least one every hour

So how about we try to make it so that 99% of the time we see at least pumpkin in an hour?

Then we need some proper maths. If P is the probability of getting a pumpkin, we need that (1-P) ^ 18000 = 0.01 – ie that if we multiple all the 18000 chances of not getting a pumpkin together we get 0.01 – so there’s a 1% chance that we won’t get one at all in the hour.

Now we solve for P (actually I’m gong to solve for P’ = (1-P) which is easier)

- Start with the equation:
- P’ ^ 18000 = 0.01

- log both sides to remove the powers:
- ln(P’ ^ 18000) = ln(0.01)

- Use the fact that ln(x ^ y) = y ln(x)
- 18000 ln(P’) = ln(0.01)

- Divide out
- ln(P’) = ln(0.01) / 18000

- Now re-apply the exponent and apply e ^ ln(x) = x
- e ^ ln(P’) = e ^ (ln(0.01) / 18000)
- P’ = e ^ (ln(0.01) / 18000)

- And calculate (can paste that into Google!)
- P’ = 0.99974
- P = 0.00026

We can choose a nicer number and validate the maths, so if we choose 0.00025, then 0.99975 ^ 18000 = 0.0111 which is fine – just over 0.01 but not significantly.

If we multiply this by 18000 we’d get the number of pumpkins we’d actually expect to see every hour – which is 4.5. So if I use this, we’ll get 4.5 an hour, but very few hours will go by without seeing one. That feels like a reasonable number to get.

I should note that this isn’t really necessary for something like this – because we always have more time available and an hour is not a significant part of that time, the fact that you don’t see one in an hour isn’t so important. However, if this was something like opening loot boxes looking for a unique item, we can use this sort of maths to set a maximum number of boxes anyone will have to open.

### Using the numbers

In Elm we have a weighted random, so what I’m going to do is pick the main numbers to add up to 1 million, and then add a 250 on to that.

So my weights are 600000 Herbs : 250000 Flowers : 150000 Mushrooms : 250 Pumpkins

This doesn’t add up perfectly, but that doesn’t matter – taking 250 of one of the other numbers wouldn’t really make a big difference to the rest of them. The reality of the situation is that I’ll tweak these numbers anyway – say if I add another foraged resource – and it’ll change when I’ve playtested with this for a while, I’ll get a feeling for if this is too high or too low. The main aim of these calculations is to get the figures into the right order of magnitude, which can be particularly tricky when we’re looking at rare chances.