[英]Pick random element from list with probability
I Have a list that contains four item (A, B, C, D). 我有一个包含四个项目(A,B,C,D)的列表。 Every item has a probability to be chosen.
每个项目都有可能被选中。 Let's say for example A has 74% of chance to be picked, B 15%, C 7% ,and D 4%.
比方说,例如A有74%的机会被选中,B 15%,C 7%和D 4%。
I want to create a function that choose randomly an item according to its probability. 我想创建一个函数,根据其概率随机选择一个项目。
Any help please? 有什么帮助吗?
Define a class for your items like this: 为您的商品定义一个类,如下所示:
class Items<T>
{
public double Probability { get; set; }
public T Item { get; set; }
}
then initialize it 然后初始化它
var initial = new List<Items<string>>
{
new Items<string> {Probability = 74 / 100.0, Item = "A"},
new Items<string> {Probability = 15 / 100.0, Item = "B"},
new Items<string> {Probability = 7 / 100.0, Item = "C"},
new Items<string> {Probability = 4 / 100.0, Item = "D"},
};
then you need to convert it to aggregate a sum of probabilities from 0 to 1 那么你需要转换它来汇总从0到1的概率之和
var converted = new List<Items<string>>(initial.Count);
var sum = 0.0;
foreach (var item in initial.Take(initial.Count - 1))
{
sum += item.Probability;
converted.Add(new Items<string> {Probability = sum, Item = item.Item});
}
converted.Add(new Items<string> {Probability = 1.0, Item = initial.Last().Item});
now you can pick an item from converted
collection with respect to probability: 现在你可以根据概率从
converted
集合中选择一个项目:
var rnd = new Random();
while (true)
{
var probability = rnd.NextDouble();
var selected = converted.SkipWhile(i => i.Probability < probability).First();
Console.WriteLine($"Selected item = {selected.Item}");
}
NOTE: my implementation have O(n)
complexity. 注意:我的实现具有
O(n)
复杂性。 You can optimize it with binary search (because values in converted
collection are sorted) 您可以使用二进制搜索对其进行优化(因为已
converted
集合中的值已排序)
My apologies for answering this one like this - I'm kinda viewing it as a sort of "Euler.Net" puzzle, and a way of playing around with Generics. 我很抱歉这样回答这个问题 - 我有点认为它是一种“Euler.Net”难题,也是一种玩弄Generics的方式。
Anyway, here's my go at it: 无论如何,这是我的理由:
public class WeightedItem<T>
{
private T value;
private int weight;
private int cumulativeSum;
private static Random rndInst = new Random();
public WeightedItem(T value, int weight)
{
this.value = value;
this.weight = weight;
}
public static T Choose(List<WeightedItem<T>> items)
{
int cumulSum = 0;
int cnt = items.Count();
for (int slot = 0; slot < cnt; slot++)
{
cumulSum += items[slot].weight;
items[slot].cumulativeSum = cumulSum;
}
double divSpot = rndInst.NextDouble() * cumulSum;
WeightedItem<T> chosen = items.FirstOrDefault(i => i.cumulativeSum >= divSpot);
if (chosen == null) throw new Exception("No item chosen - there seems to be a problem with the probability distribution.");
return chosen.value;
}
}
Usage: 用法:
WeightedItem<string> alice = new WeightedItem<string>("alice", 1);
WeightedItem<string> bob = new WeightedItem<string>("bob", 1);
WeightedItem<string> charlie = new WeightedItem<string>("charlie", 1);
WeightedItem<string> diana = new WeightedItem<string>("diana", 4);
WeightedItem<string> elaine = new WeightedItem<string>("elaine", 1);
List<WeightedItem<string>> myList = new List<WeightedItem<string>> { alice, bob, charlie, diana, elaine };
string chosen = WeightedItem<string>.Choose(myList);
using System;
public class Test{
private static String[] values = {"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","D","D","D","D",};
private static Random PRNG = new Random();
public static void Main(){
Console.WriteLine( values[PRNG.Next(values.Length)] );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.