RNGesus 1.0.3

dotnet add package RNGesus --version 1.0.3                
NuGet\Install-Package RNGesus -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RNGesus" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RNGesus --version 1.0.3                
#r "nuget: RNGesus, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RNGesus as a Cake Addin
#addin nuget:?package=RNGesus&version=1.0.3

// Install RNGesus as a Cake Tool
#tool nuget:?package=RNGesus&version=1.0.3                

RNGesus

RNGesus is a small pseudo random drop generator.

It allows you to define a pool of items (class instances), each with a given rarity and drop chance and then pick one at random.

How does it work?

RNGesus tries to pick an item from the pool with a rarity >= 1. If no drop was generated it will pick one item with rarity=0 at random, ignoring it's drop chance (Fallback).

Let's assume you have the following rarities[^1]:

Rarity Integer value
Common 0
Rare 1
Epic 2

[^1]: RNGesus uses byte values for the rarity. The higher the number, the more rare the item. This way you can define up to 256 rarities 😲

For each rarity type there are X items, each with a specific drop chance:

Item name Rarity Drop chance
Sword Common ignored
Axe Common ignored
Rare Sword Rare 50
Rare Axe Rare 25
Epic Sword Epic 50
Epic Axe Epic 25

RNGesus will now check the drops in the following order:

  1. Check if next drop should be Epic
  2. If so, check each Epic item's drop chance against RNG ⇒ If lucky, return item
  3. If nothing found, check if next drop should be Rare
  4. If so, check each Rare item's drop chance against RNG ⇒ If lucky, return item
  5. No special drop done, so choose one Common item (fallback) and return it. RNGesus will not look at it's dropchance and relies purely on System.Runtime.Random

Usage

Create your Items you want to drop and implement the IDroppable interface, e.g:

public class Item : IDroppable
{
    public double DropChance { get; set;}
    public byte Rarity { get; set; }

    public string Name { get; set; } = string.Empty;
}

Create a list of some items:

List<Item> items = new List<Item>()
{
    new Item() { Name = "Common I", Rarity = 0 }, //Common
    new Item() { Name = "Common II", Rarity = 0 },//Common
    new Item() { Name = "Common III", Rarity = 0 },//Common
    new Item() { Name = "Rare I", Rarity = 1, DropChance = 40 },
    new Item() { Name = "Rare II", Rarity = 1, DropChance = 20 },
    new Item() { Name = "Epic I", Rarity = 2, DropChance = 35},
    new Item() { Name = "Epic II", Rarity = 2, DropChance = 20},
    new Item() { Name = "Legendary I", Rarity = 3, DropChance = 10},
    new Item() { Name = "Legendary II", Rarity = 3, DropChance = 0.01},
}

Create an instance of RNGesus and assign the item pool:

RNGesus<Item> rng = new(items, new Random());

Drop a new item:

var droppedItem = rng.Next();

That's it 🙃 See the ConsoleTest Project in the repository for a more complete example.

Rarity Factors

RNGesus generates a default factor for each rarity there is. This factor is the overall chance of items with this rarity get dropped.

You can overwrite the factors like this:

rngesus.RarityFactors[0] = 20;
rngesus.RarityFactors[1] = 10;
rngesus.RarityFactors[2] = 0.01;
//...

The default values are 50d / x where x is the rarity. Rarity 0 defaults to 100d and should not be changed, otherwise you may run into a NullReferenceException.

Rarity Default factor
0 100
1 50
2 25
3 16.666
4 12.5
... ...

Luck

The RNGesus.Next() method takes an optional double parameter called luck. It adds to each item's drop chance. This is an easy way to increase the chance of dropping items with a higher rarity:

var droppedItem = rng.Next(10d);

Example with100,000 drops

The ConsoleTest programm generates 100,000 drops and these are the results of one run with the following rarity factors:

  • Common ⇒ 100
  • Uncommon ⇒ 80
  • Rare ⇒ 35
  • Epic ⇒ 10
  • Legendary ⇒ 0.01
Rarity Drop Count
Common 39781
Uncommon 47643
Rare 10179
Epic 2392
Legendary 5
Item Drop Count
Common I 8146
Common II 7879
Common III 7898
Common IV 7896
Common V 7962
Uncommon I 26410
Uncommon II 17618
Uncommon III 3615
Rare I 6329
Rare II 2932
Rare III 918
Epic I 1456
Epic II 715
Epic III 220
Epic IV 1
Legendary I 3
Legendary II 2
Legendary III 0
Legendary IV 0
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 439 8/8/2022