How to generate a random weighted distribution of elements

前端 未结 4 369
执笔经年
执笔经年 2021-01-13 07:04

I would like to return an array which has a set of unique elements randomly distributed according to custom frequency. My real world use-case is the repetition of carousel i

4条回答
  •  日久生厌
    2021-01-13 07:20

    There is a very simple solution. The random() method returns a number between 0 and 1 inclusive.

    Eg if the number returned is > 0.2, then output C (ie 80% chance).

提交回复
热议问题