https://github.com/michaldivis/dark-random-utils
C# randomization helpers - random picker, weighted picks
https://github.com/michaldivis/dark-random-utils
random-picker randomizer weighted-random
Last synced: about 1 year ago
JSON representation
C# randomization helpers - random picker, weighted picks
- Host: GitHub
- URL: https://github.com/michaldivis/dark-random-utils
- Owner: michaldivis
- License: mit
- Created: 2022-09-07T09:25:55.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-20T05:18:28.000Z (over 3 years ago)
- Last Synced: 2025-05-15T17:13:50.412Z (about 1 year ago)
- Topics: random-picker, randomizer, weighted-random
- Language: C#
- Homepage:
- Size: 407 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README

# Dark Random Utils
C# randomization helpers - random picker, weighted picks, loop.
## Nuget
[](https://www.nuget.org/packages/Divis.DarkRandomUtils/)
DarkRandomUtils is available using [nuget](https://www.nuget.org/packages/Divis.DarkRandomUtils/). To install DarkRandomUtils, run the following command in the [Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console)
```Powershell
PM> Install-Package Divis.DarkRandomUtils
```
## Random Picker
```csharp
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[]
{
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
```csharp
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
```