To do this you have to associate a weight to each possibility. It doesn't have to be a percentage.
This would be an example of a generic item with a weight
public class WeightedItem<T>
{
public T Item { get; set; }
public int Weight { get; set; }
public WeightedItem(T item, int weight=1)
{
Item = item;
Weight = weight;
}
}
To pick a random one of all your items you just give items with a higher weight a better chance
public static class WeightedRandomizer<T>
{
private static System.Random _random;
static WeightedRandomizer()
{
_random = new System.Random();
}
public static T PickRandom(List<WeightedItem<T>> items)
{
int totalWeight = items.Sum(item => item.Weight);
int randomValue = _random.Next(1, totalWeight);
int currentWeight = 0;
foreach (WeightedItem<T> item in items)
{
currentWeight += item.Weight;
if (currentWeight >= randomValue)
return item.Item;
}
return default(T);
}
}
For example:
var candidates = new List<WeightedItem<string>>
{
new WeightedItem<string>("Never", 0),
new WeightedItem<string>("Rarely", 2),
new WeightedItem<string>("Sometimes", 10),
new WeightedItem<string>("Very often", 50),
};
for (int i = 0; i < 100; i++)
{
Debug.WriteLine(WeightedRandomizer<string>.PickRandom(candidates));
}
The chances for these items would be:
"Never" : 0 of 62 times (0%)
"Rarely" : 2 of 62 times (3.2%)
"Sometimes" : 10 of 62 times (16.1%)
"Very often": 50 of 62 times (80.6%)
Instead of strings you can of course use any other type like an image, number or a class of your own.