Weighted random numbers

前端 未结 6 753
情书的邮戳
情书的邮戳 2020-11-22 08:50

I\'m trying to implement a weighted random numbers. I\'m currently just banging my head against the wall and cannot figure this out.

In my project (Hold\'em hand-ran

6条回答
  •  难免孤独
    2020-11-22 09:22

    Build a bag (or std::vector) of all the items that can be picked.
    Make sure that the number of each items is proportional to your weighting.

    Example:

    • 1 60%
    • 2 35%
    • 3 5%

    So have a bag with 100 items with 60 1's, 35 2's and 5 3's.
    Now randomly sort the bag (std::random_shuffle)

    Pick elements from the bag sequentially until it is empty.
    Once empty re-randomize the bag and start again.

提交回复
热议问题