AndyCosta.WeightedRandom
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package AndyCosta.WeightedRandom --version 1.0.0
NuGet\Install-Package AndyCosta.WeightedRandom -Version 1.0.0
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="AndyCosta.WeightedRandom" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AndyCosta.WeightedRandom --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AndyCosta.WeightedRandom, 1.0.0"
#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 AndyCosta.WeightedRandom as a Cake Addin #addin nuget:?package=AndyCosta.WeightedRandom&version=1.0.0 // Install AndyCosta.WeightedRandom as a Cake Tool #tool nuget:?package=AndyCosta.WeightedRandom&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
WeightedRandom
A concise C# library for effortless random selection of items from a list, each with its own probability. Perfect for scenarios requiring diverse likelihoods in a simple and efficient package.
Getting Started
Usage
Various examples have been provided in this repo using CLI, WPF, etc.
In a nutshell:
- Create an array of
Option<T>
.- Each option consists of a
Value
of typeT
and an integer representing itsWeight
. - For example:
new Option<char>('A', 10)
creates an Option for the characterA
with a weight of 10. - Weights are relative to one another. For example, the set of weights [10, 20, 40] is equivalent to [1, 2, 4] or [30, 40, 120], they all give the first entry a 14% change, the second entry a 28% chance, and the third entry a 57% chance.
- Each option consists of a
- Give that array to a new instance of
WeightedRandomizer<T>
.- You may optionally provide a specific seed value as well. Otherwise the default seed value for
System.Random
is used
- You may optionally provide a specific seed value as well. Otherwise the default seed value for
- Call the method
Next()
and consume the returned value however you wish.
var options = new []
{
new Option<char>('A', 10),
new Option<char>('B', 30),
new Option<char>('C', 10),
};
var weightedRandom = new WeightedRandomizer<char>(options);
Option<char> selection = weightedRandom.Next();
Version History
- 0.1
- Initial Release
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.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.