How can I create a specified amount of random values that all equal up to a specified number in PHP?

后端 未结 6 1995
庸人自扰
庸人自扰 2021-01-02 08:18

For example, say I enter \'10\' for the amount of values, and \'10000\' as a total amount.

The script would need to randomize 10 different numbers that all equal up

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 08:54

    maybe something like this:

    set max amount remaining to the target number

    loop for 1 to the number of values you want - 1

    get a random number from 0 to the max amount remaining

    set new max amount remaining to old max amount remaining minus the current random number

    repeat loop

    you will end up with a 'remainder' so the last number is determined by whatever is left over to make up the original total.

提交回复
热议问题