从C#中的加权列表中选择x个随机元素(无需替换)

Gab*_*iel 6 c# statistics probability

更新:我的问题已经解决,我在我的问题中更新了代码源以匹配Jason的答案.请注意,rikitikitik的答案是解决从更换样品中挑选卡片的问题.

我想从加权列表中选择x个随机元素.取样无需更换.我找到了这个答案:https://stackoverflow.com/a/2149533/57369,带有Python实现.我在C#中实现了它并进行了测试.但结果(如下所述)与我的预期不符.我不了解Python,所以我很确定在将代码移植到C#时犯了一个错误,但是我无法看到Pythong中的代码在哪里得到了很好的记录.

我选了一张卡10000次,这是我获得的结果(结果是一致的执行):

Card 1: 18.25 % (10.00 % expected)
Card 2: 26.85 % (30.00 % expected)
Card 3: 46.22 % (50.00 % expected)
Card 4: 8.68 % (10.00 % expected)
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,卡1和卡4的重量均为1,但是卡1比卡4更经常被选中(即使我选择了2或3张卡).

测试数据:

var cards = new List<Card>
{
    new Card { Id = 1, AttributionRate = 1 }, // 10 %
    new Card { Id = 2, AttributionRate = 3 }, // 30 %
    new Card { Id = 3, AttributionRate = 5 }, // 50 %
    new Card { Id = 4, AttributionRate = 1 }, // 10 %
};
Run Code Online (Sandbox Code Playgroud)

这是我在C#中的实现

public class CardAttributor : ICardsAttributor
{
    private static Random random = new Random();

    private List<Node> GenerateHeap(List<Card> cards)
    {
        List<Node> nodes = new List<Node>();
        nodes.Add(null);

        foreach (Card card in cards)
        {
            nodes.Add(new Node(card.AttributionRate, card, card.AttributionRate));
        }

        for (int i = nodes.Count - 1; i > 1; i--)
        {
            nodes[i>>1].TotalWeight += nodes[i].TotalWeight;
        }

        return nodes;
    }

    private Card PopFromHeap(List<Node> heap)
    {
        Card card = null;

        int gas = random.Next(heap[1].TotalWeight);
        int i = 1;

        while (gas >= heap[i].Weight)
        {
            gas -= heap[i].Weight;
            i <<= 1;

            if (gas >= heap[i].TotalWeight)
            {
                gas -= heap[i].TotalWeight;
                i += 1;
            }
        }

        int weight = heap[i].Weight;
        card = heap[i].Value;

        heap[i].Weight = 0;

        while (i > 0)
        {
            heap[i].TotalWeight -= weight;
            i >>= 1;
        }

        return card;
    }

    public List<Card> PickMultipleCards(List<Card> cards, int cardsToPickCount)
    {
        List<Card> pickedCards = new List<Card>();

        List<Node> heap = GenerateHeap(cards);

        for (int i = 0; i < cardsToPickCount; i++)
        {
            pickedCards.Add(PopFromHeap(heap));
        }

        return pickedCards;
    }
}

class Node
{
    public int Weight { get; set; }
    public Card Value { get; set; }
    public int TotalWeight { get; set; }

    public Node(int weight, Card value, int totalWeight)
    {
        Weight = weight;
        Value = value;
        TotalWeight = totalWeight;
    }
}

public class Card
{
    public int Id { get; set; }
    public int AttributionRate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Jas*_*rff 3

该程序有两个小错误。首先,随机数的范围应该恰好等于所有物品的总重量:

int gas = random.Next(heap[1].TotalWeight);
Run Code Online (Sandbox Code Playgroud)

其次,更改说的两个gas >地方gas >=

(原始 Python 代码没问题,因为gas是浮点数,因此>和之间的差异>=可以忽略不计。编写该代码是为了接受整数或浮点权重。)

更新:好的,您在代码中进行了建议的更改。我认为该代码现在是正确的!