Skip to content
This repository has been archived by the owner on Oct 20, 2018. It is now read-only.

jonsagara/WeightedRandom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WeightedRandom

The WeightedRandom and WeightedRandomItem classes allow you assign weights to a finite set of items, and then randomly select those items. The frequency with which they are returned varies according to the weights you assign them.

Usage

var items = new WeightedRandomItem<string>[]
{
    new WeightedRandomItem<string> ("A", 50),
    new WeightedRandomItem<string> ("B", 40),
    new WeightedRandomItem<string> ("C", 10)
};

var weightedRandom = new WeightedRandom<string>(items);

// Call Next() repeatedly to get items.
// Over time, A will be returned ~50% of the time; B 40%; and C 10%.
string itemId = weightedRandom.Next();

Notes

There is a private WeightedRandom constructor that allows you to specify a distribution size. I had intended on making it public, but have since had second thoughts -- it's easier to use this class if you leave the distribution size at 100. I may refactor and remove that constructor in the future.

About

A class library that produces a weighted random distribution of a set of items/weights.

Resources

License

Stars

Watchers

Forks

Packages

No packages published