Drop Rate: Difference between revisions
| Line 5: | Line 5: | ||
<syntaxhighlight lang="cs"> | <syntaxhighlight lang="cs"> | ||
// 1 in | // 1 in 20 drop rate (could also set to 0.05f directly) | ||
float DropRate = 1f / | float DropRate = 1f / 20f; | ||
void OnEnemyKill() | void OnEnemyKill() | ||
Revision as of 20:38, 9 June 2024
A Drop Rate is a value that indicates how often an item has a chance to drop from a game event such as defeating an enemy.
Simple Drop Rate
The simplest implementation of a drop rate is to store the rate as a percentage or ratio, and then generate a random number and check if it is lower than the drop rate. If it is, the item drops. This is similar to rolling a dice and dropping the item if certain numbers come up.
// 1 in 20 drop rate (could also set to 0.05f directly)
float DropRate = 1f / 20f;
void OnEnemyKill()
{
var n = random.NextDouble();
if (n < DropRate)
{
DropItem();
}
}
Sampling without Replacement
For rare items that the player cannot trade or must obtain for progression, sampling without replacement provides a more consistent experience for obtaining an item. Each time the item fails to drop, the chance of failure decreases. This is similar to picking a card from a deck and removing the card from the deck once picked.
// 1 in 20 drop rate (numerator)
float DropRateSuccess = 1f;
// 1 in 20 drop rate (denominator)
int DropRateTotal = 20;
// number of times the item has failed to drop since last success
int DropFailures = 0;
void OnEnemyKill()
{
var n = random.NextDouble();
if (n < DropRateSuccess / (DropRateTotal - DropFailures))
{
DropItem();
DropFailures = 0;
}
else
{
DropFailures++;
}
}
Example: Dropping a key for a door
Suppose a game has a room with a locked door that spawns endless waves of monsters. Killing a monster has a 10% chance to drop a key that opens the door. How many monsters need to be killed to open the door?
| Total Kills | Simple Drop Rate | Sampling without Replacement |
|---|---|---|
| Players w/ Key | Players w/ Key | |
| 1 | 10% | 10% |
| 2 | 19% | 20% |
| 3 | 27% | 30% |
| 4 | 34% | 40% |
| 5 | 41% | 50% |
| 6 | 47% | 60% |
| 7 | 52% | 70% |
| 8 | 57% | 80% |
| 9 | 61% | 90% |
| 10 | 65% | 100% |
| 20 | 88% | |
| 30 | 96% | |
| 40 | 99% |