【发布时间】:2015-09-22 14:48:53
【问题描述】:
我想生成 4 个介于 1 和 100 之间的随机数,这样总和为 100。我该怎么做?
说结果1) 20,10,40,30 结果2) 33,33,33,1 等谢谢
【问题讨论】:
-
查看这个答案。它不是 Swift,但它解释了算法。 stackoverflow.com/a/18600737/1630618
标签: swift arc4random
我想生成 4 个介于 1 和 100 之间的随机数,这样总和为 100。我该怎么做?
说结果1) 20,10,40,30 结果2) 33,33,33,1 等谢谢
【问题讨论】:
标签: swift arc4random
要走的路是:
Step 1: Generate four random numbers between 0 and 1
Step 2: Add these four numbers
Step 3: Divide each of the four numbers by the sum,
Step 4: Multiply by 100, and round to the nearest integer.
【讨论】: