AndyCosta.WeightedRandom 1.0.0

There is a newer version of this package available.
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                
#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                

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:

  1. Create an array of Option<T>.
    • Each option consists of a Value of type T and an integer representing its Weight.
    • For example: new Option<char>('A', 10) creates an Option for the character A 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.
  2. 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
  3. 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 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.

Version Downloads Last updated
1.0.1 281 12/7/2023
1.0.0 145 12/6/2023