【发布时间】:2015-07-22 15:11:29
【问题描述】:
举个例子。我想随机分发 100 块巧克力给 25 个孩子。我不能给任何孩子超过 10 块巧克力。
所以这里 m = 100,n = 25,x = 1 和 y = 12。
我已经检查了这些问题。
Dividing a number into m parts uniformly randomly
Dividing a number into random unequal parts
他们确实给出了一些想法,但在这些问题中没有指定 x 和 y。
所以基本上,
1) 巧克力总数 = 100
2) 我只能给每个孩子至少 1 块和最多 12 块巧克力
3) 巧克力应该分发给 25 个孩子
4) 我不想要任何分布(均匀或正态) - 它应该是纯随机的。 (如果一切都失败了,我愿意排除这种情况。)
Private Function divideUniformlyRandomly(n As Integer, m As Integer) As Integer()
Dim rRandom As New Random
Dim fences As Integer() = New Integer(m - 2) {}
For i As Integer = 0 To m - 3
fences(i) = rRandom.Next(0, n - 1)
Next
[Array].Sort(fences)
Dim result As Integer() = New Integer(m - 1) {}
result(0) = fences(0)
For i As Integer = 1 To m - 3
result(i) = fences(i + 1) - fences(i)
Next
result(m - 1) = n - 1 - fences(m - 2)
Return result
End Function
这确实有效,但我也得到 0 和 13。我不能确保 x 和 y 在这里。
【问题讨论】:
-
先给我们你的尝试。
-
“我不想要任何分布(均匀或正态)——它应该是纯粹随机的。”——这没有意义。