在范围内生成3个随机数并且总和3个数字=此范围[重复]

Possible Duplicate:
How to make 5 random numbers with sum of 100

i am new with php . Can i ask how to make 3 random number with range an sum of 3 numbers = max value of this range.

Example : i have range 0->900; 3 random number is 213 437 250

1个回答



选择0到900之间的两个随机数。 将0和900添加到列表中。 排序他们。 你的数字是连续数字之间的3个差异。</ p>

例如,你选择503和117.你的排序列表是0,117,503,900。所以你的差异是:</ p>

117 - 0 = 117

503 - 117 = 386

900 - 503 = 397 </ p>

所以你的三个数字是117, 386和397。</ p>

由于您只需要三个数字,因此可以简化过程:生成0到900(含)之间的两个随机数。 调用较大的A和较小的B.您的数字是 B </ code>, A - B </ code>和 900 - A </ code>。</ p>
\ n

请参阅此处。</ p>
</ div>

展开原文

原文

Pick two random numbers between 0 and 900 inclusive. Add 0 and 900 to the list. Sort them. Your numbers are the 3 differences between successive numbers.

For example, say you pick 503 and 117. Your sorted list is 0, 117, 503, 900. So your differences are:

117 - 0 = 117
503 - 117 = 386
900 - 503 = 397

So your three numbers are 117, 386, and 397.

Since you only need three numbers, you can simplify the process: Generate two random numbers between 0 and 900 (inclusive). Call the larger A and the smaller B. Your numbers are B, A - B and 900 - A.

See here.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐