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

后端 未结 6 2015
庸人自扰
庸人自扰 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:29

    Generate 10 random numbers till 10000 . Sort them from big to small : g0 to g9

    g0 = 10000 - r0
    g1 = r0 - r1
    ...
    g8 = r8 - r9
    g9 = r9
    

    This will yield 10 random numbers over the full range which add up to 10000.

提交回复
热议问题