Generating random results by weight in PHP?

前端 未结 12 731
青春惊慌失措
青春惊慌失措 2020-11-22 07:35

I know how to generate a random number in PHP but lets say I want a random number between 1-10 but I want more 3,4,5\'s then 8,9,10\'s. How is this possible? I would post wh

12条回答
  •  失恋的感觉
    2020-11-22 08:15

    The naive hack for this would be to build a list or array like

    1, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 10

    And then select randomly from that.

提交回复
热议问题