Drop Rate: Difference between revisions

From C# Gamedev Wiki
Line 51: Line 51:
{|
{|
|+Simple Drop Rate
|+Simple Drop Rate
!style="width: 60px;" | #Kills
!Total Kills
!style="width: 120px;" | %Players w/ Key
!Players w/ Key
|-
|-
|1 || 10%
|1 || 10%
Line 82: Line 82:


{|
{|
|+Simple Drop Rate
|+Sampling without Replacement
!style="width: 60px;" | #Kills
!Total Kills
!style="width: 120px;" | %Players w/ Key
!Players w/ Key
|-
|-
|1 || 10%
|1 || 10%
|-
|-
|2 || 11%
|2 || 20%
|-
|-
|3 || 13%
|3 || 30%
|-
|-
|4 || 34%
|4 || 40%
|-
|-
|5 || 41%
|5 || 50%
|-
|-
|6 || 47%
|6 || 60%
|-
|-
|7 || 52%
|7 || 70%
|-
|-
|8 || 57%
|8 || 80%
|-
|-
|9 || 61%
|9 || 90%
|-
|-
|10 || 65%
|10 || 100%
|-
|20 || 88%
|-
|30 || 96%
|-
|40 || 99%
|}
|}


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

Revision as of 19:59, 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?

Simple Drop Rate
Total 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%
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%