I have a requirement in my .NET project where I need to select an item from a collection, each item has a Weight (integer from 1 to 10) assigned to it.
I need a random generator that would take this weight into consideration i.e., the higher the weight, the more chances the object would be selected.
Quick copy/paste C# code in case someone stumbles upon this.
class RandomWeightedSelector<T>
{
private List<T> items = new List<T>();
public void Add(T item, uint weight = 1)
{
for (int i = 0; i < weight; i++)
items.Add(item);
}
public T GetRandom()
{
return items[new Random().Next(0, items.Count)];
}
}
Weighted random choices mean selecting random elements from a list or an array by the probability of that element. We can assign a probability to each element and according to that element(s) will be selected. By this, we can select one or more than one element from the list, And it can be achieved in two ways.
Here's an algorithm which doesn't require adding the items multiple times to a list. It can also work with non-integer weights, although if you're using NextDouble from System.Random, you'll have to scale all of the weights to add up to 1, or multiply the value from NextDouble with S to get it in the desired range.
Given a list L of items (I,W), where I is the item and W is the weight:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With