Weighted random selection from array

后端 未结 13 1771
醉酒成梦
醉酒成梦 2020-11-28 03:13

I would like to randomly select one element from an array, but each element has a known probability of selection.

All chances together (within the array) sums to 1.<

相关标签:
13条回答
  • 2020-11-28 03:38

    If the array is small, I would give the array a length of, in this case, five and assign the values as appropriate:

    array[
        0 => 0
        1 => 0
        2 => 0
        3 => 0
        4 => 1
    ]
    
    0 讨论(0)
提交回复
热议问题