Drop Rate: Difference between revisions
(Created page with "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. <syntaxhighlight lang="cs"> // 1 in 4 drop...") |
|||
| Line 50: | Line 50: | ||
{| | {| | ||
!#Kills | |+Simple Drop Rate | ||
!%Players w/ Key | !style="width: 60px;" | #Kills | ||
!style="width: 120px;" | %Players w/ Key | |||
|- | |- | ||
|1 || 10% | |1 || 10% | ||
| Line 58: | Line 59: | ||
|- | |- | ||
|3 || 27% | |3 || 27% | ||
|- | |||
|4 || 34% | |||
|- | |||
|5 || 41% | |||
|- | |||
|6 || 47% | |||
|- | |||
|7 || 52% | |||
|- | |||
|8 || 57% | |||
|- | |||
|9 || 61% | |||
|- | |||
|10 || 65% | |||
|- | |||
|20 || 88% | |||
|- | |||
|30 || 96% | |||
|- | |||
|40 || 99% | |||
|} | |||
{| | |||
|+Simple Drop Rate | |||
!style="width: 60px;" | #Kills | |||
!style="width: 120px;" | %Players w/ Key | |||
|- | |||
|1 || 10% | |||
|- | |||
|2 || 11% | |||
|- | |||
|3 || 13% | |||
|- | |- | ||
|4 || 34% | |4 || 34% | ||
Revision as of 19:55, 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 4 drop rate (could also set to 0.25f directly)
float DropRate = 1f / 4f;
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, we remove the chance of failure from the pool. 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
int NumDropFailures = 0;
void OnEnemyKill()
{
var n = random.NextDouble();
if (n < DropRateSuccess / (DropRateTotal - NumDropFailures))
{
DropItem();
DropFailures = 0;
}
else
{
DropFailures++;
}
}
Example: Dropping a key for a door
Let's say a game has a room that spawns endless waves of monsters and a locked door. 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?
| #Kills | %Players w/ Key |
|---|---|
| 1 | 10% |
| 2 | 19% |
| 3 | 27% |
| 4 | 34% |
| 5 | 41% |
| 6 | 47% |
| 7 | 52% |
| 8 | 57% |
| 9 | 61% |
| 10 | 65% |
| 20 | 88% |
| 30 | 96% |
| 40 | 99% |
| #Kills | %Players w/ Key |
|---|---|
| 1 | 10% |
| 2 | 11% |
| 3 | 13% |
| 4 | 34% |
| 5 | 41% |
| 6 | 47% |
| 7 | 52% |
| 8 | 57% |
| 9 | 61% |
| 10 | 65% |
| 20 | 88% |
| 30 | 96% |
| 40 | 99% |