selection based on percentage weighting

后端 未结 13 2144
忘掉有多难
忘掉有多难 2020-12-04 13:40

I have a set of values, and an associated percentage for each:

a: 70% chance
b: 20% chance
c: 10% chance

I want to select a value (a, b, c) based

13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 13:52

    1. Let T = the sum of all item weights
    2. Let R = a random number between 0 and T
    3. Iterate the item list subtracting each item weight from R and return the item that causes the result to become <= 0.

提交回复
热议问题