Drop Rate: Difference between revisions

From C# Gamedev Wiki
Line 49: Line 49:


{|
{|
|+Simple Drop Rate
!Total Kills
!Total Kills
!Players w/ Key
!Simple Drop Rate
!Sampling without Replacement
|-
|-
|1 || 10%
| || Players w/ Key || Players w/ Key
|-
|-
|2 || 19%
|1 || 10% || 10%
|-
|-
|3 || 27%
|2 || 19% || 20%
|-
|-
|4 || 34%
|3 || 27% || 30%
|-
|-
|5 || 41%
|4 || 34% || 40%
|-
|-
|6 || 47%
|5 || 41% || 50%
|-
|-
|7 || 52%
|6 || 47% || 60%
|-
|-
|8 || 57%
|7 || 52% || 70%
|-
|-
|9 || 61%
|8 || 57% || 80%
|-
|-
|10 || 65%
|9 || 61% || 90%
|-
|-
|20 || 88%
|10 || 65% || 100%
|-
|-
|30 || 96%
|20 || 88% ||
|-
|-
|40 || 99%
|30 || 96% ||  
|}
 
{|
|+Sampling without Replacement
!Total Kills
!Players w/ Key
|-
|1 || 10%
|-
|2 || 20%
|-
|3 || 30%
|-
|4 || 40%
|-
|5 || 50%
|-
|6 || 60%
|-
|7 || 70%
|-
|8 || 80%
|-
|9 || 90%
|-
|-
|10 || 100%
|40 || 99% ||
|}
|}


[[Category:Loot]]
[[Category:Loot]]
[[Category:Statistics]]
[[Category:Statistics]]

Revision as of 20:07, 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, 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
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?

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%