Я думаю, что вы ищете генератор взвешенных случайных чисел.
Так что я думаю, что вы должны быть в состоянии принять это решение для ваших нужд.
Во-первых, весовой классопределить свой вес.Это было бы то, что вы использовали бы для своих сторон кости.
public class WeightUnit
{
public string Name = string.Empty;
public int Weight = 0;
public WeightUnit(string n, int w)
{
this.Name = n;
this.Weight = w;
}
}
Тогда этот метод вернет взвешенное случайное число:
public static WeightUnit GetRandom(List<WeightUnit> weights)
{
var totalWeight = weights.Sum(x => x.Weight);
int randomNumber = _rnd.Next(0, totalWeight);
WeightUnit selected = null;
foreach (WeightUnit unit in weights)
{
if (randomNumber < unit.Weight)
{
selected = unit;
break;
}
randomNumber = randomNumber - unit.Weight;
}
return selected;
}
А вот основная программа для проверки:
private static Random _rnd = new Random();
static void Main()
{
List<WeightUnit> weights = new List<WeightUnit>
{
new WeightUnit("1", 20),
new WeightUnit("2", 20),
new WeightUnit("3", 20),
new WeightUnit("4", 10),
new WeightUnit("5", 10),
new WeightUnit("6", 10)
};
Dictionary<string, int> result = new Dictionary<string, int>();
WeightUnit selected = null;
for (int i = 0; i < 1000; i++)
{
selected = GetRandom(weights);
if (selected != null)
{
if (result.ContainsKey(selected.Name))
{
result[selected.Name] = result[selected.Name] + 1;
}
else
{
result.Add(selected.Name, 1);
}
}
}
Console.WriteLine("1\t\t" + result["1"]);
Console.WriteLine("2\t\t" + result["2"]);
Console.WriteLine("3\t\t" + result["3"]);
Console.WriteLine("4\t\t" + result["4"]);
Console.WriteLine("5\t\t" + result["5"]);
Console.WriteLine("6\t\t" + result["6"]);
Console.ReadLine();
}
Как видите, я назначил удвоенное значение веса для чисел 1,2 и 3, и более 1000 итераций выбирается в два раза больше по сравнению с 4, 5 и5. Поэтому, когда вы составляете свой список, вы можете назначить нужный вам вес.