Generate A Weighted Random Number

前端 未结 11 2310
予麋鹿
予麋鹿 2020-11-22 12:40

I\'m trying to devise a (good) way to choose a random number from a range of possible numbers where each number in the range is given a weight. To put it simply: given the

11条回答
  •  野性不改
    2020-11-22 12:55

    How about

    int [ ] numbers = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 } ;

    then you can randomly select from numbers and 0 will have an 80% chance, 1 10%, and 2 10%

提交回复
热议问题