Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ndsvw/fluent-random-picker
Fluent Random Picker is a nice, performant, fluent way to pick random values. Probabilities can be specified, values can be weighted.
https://github.com/ndsvw/fluent-random-picker
choose csharp fluent hacktoberfest hacktoberfest2023 outof pick picker probability random
Last synced: 7 days ago
JSON representation
Fluent Random Picker is a nice, performant, fluent way to pick random values. Probabilities can be specified, values can be weighted.
- Host: GitHub
- URL: https://github.com/ndsvw/fluent-random-picker
- Owner: ndsvw
- License: mit
- Created: 2021-04-15T06:22:33.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-13T20:23:08.000Z (17 days ago)
- Last Synced: 2025-01-17T07:04:17.689Z (14 days ago)
- Topics: choose, csharp, fluent, hacktoberfest, hacktoberfest2023, outof, pick, picker, probability, random
- Language: C#
- Homepage: https://www.nuget.org/packages/FluentRandomPicker
- Size: 354 KB
- Stars: 40
- Watchers: 3
- Forks: 3
- Open Issues: 8
-
Metadata Files:
- Readme: README-Advanced.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluent Random Picker: Advanced
## Specifying a seed
```c#
var seed = 1234567;var value1 = Out.Of(seed).Values(new[] { 1, 2, 3, 4 }).PickOne();
var value2 = Out.Of(seed).Values(new[] { 1, 2, 3, 4 }).PickOne();
// value1 and value2 are always equal.
```## Using a secure RNG
The default random number generator uses System.Random, which is not cryptographically secure.This is how you make Fluent Random Picker use an implementation based on the [cryptographically strong RandomNumberGenerator class](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator?view=net-7.0).
```c#
var secureRng = new FluentRandomPicker.Random.SecureRandomNumberGenerator();
var value = Out.Of(secureRng).Values(new[] { 1, 2, 3, 4 }).PickOne();
```## Using a custom RNG
Create a class that implements `IRandomNumberGenerator` and pass it to the `Of()` method:
```c#
public class MyCustomRandomNumberGenerator : IRandomNumberGenerator
{
public double NextDouble()
{
// ...
}public int NextInt()
{
// ...
}public int NextInt(int n)
{
// ...
}public int NextInt(int min, int max)
{
// ...
}
}var customRng = new MyCustomRandomNumberGenerator();
var value = Out.Of(customRng).Values(new[] { 1, 2, 3, 4 }).PickOne();
// value gets picked via a specified random number generator.
```