Generating random results by weight in PHP?

前端 未结 12 698
青春惊慌失措
青春惊慌失措 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:03

    There's a pretty good tutorial for you.

    Basically:

    1. Sum the weights of all the numbers.
    2. Pick a random number less than that
    3. subtract the weights in order until the result is negative and return that number if it is.

提交回复
热议问题