Skip to content

C# randomization helpers - random picker, weighted picks

License

Notifications You must be signed in to change notification settings

michaldivis/dark-random-utils

Repository files navigation

Dark Random Utils

C# randomization helpers - random picker, weighted picks, loop.

Nuget

Nuget

DarkRandomUtils is available using nuget. To install DarkRandomUtils, run the following command in the Package Manager Console

PM> Install-Package Divis.DarkRandomUtils

Random Picker

var picker = new RandomPicker(new Random(123456));

var names = new[] { "Bjorn", "Helmut", "Nabeel", "Amelia" };
Console.WriteLine($"Names: {string.Join(", ", names)}");

var randomName = picker.Pick(names);
Console.WriteLine($"Randomly picked name: {randomName}");

//output:
//Randomly picked name: Helmut

var weightedNames = new WeightedPick<string>[] 
{ 
    new("Bjorn", 3), //30% chance of getting picked
    new("Helmut", 1), //10% chance of getting picked
    new("Nabeel", 2), //20% chance of getting picked
    new("Amelia", 4) //40% chance of getting picked
};

var weightedRandomName = picker.PickWeighted(weightedNames);
Console.WriteLine($"Randomly picked weighted name: {weightedRandomName}");

//output:
//Randomly picked weighted name: Bjorn

Loop

var names = new[] { "Bjorn", "Helmut", "Nabeel", "Amelia" };
Console.WriteLine($"Names: {string.Join(", ", names)}");

var loop = Loop.Create(names);

for (int i = 0; i < 10; i++)
{
    Console.WriteLine($"{i}: {loop.GetNext()}");
}

//output:
//0: Bjorn
//1: Helmut
//2: Nabeel
//3: Amelia
//4: Bjorn
//5: Helmut
//6: Nabeel
//7: Amelia
//8: Bjorn
//9: Helmut