例如生成10个0到20之间的随机数,和为100:
Dim lst As new List(of Integer)
For i As Integer= 0 To 9
lst.add(rand.Next(0,20))
Next
Dim rd As Integer = lst.sum - 100
Dim sp As Integer = iif(rd>0, -1,1)
Do While rd <> 0
Dim idx As Integer = rand.Next(1,10)
If lst(idx) + sp >= 0 Then
lst(idx) = lst(idx) + sp
rd = rd + sp
End If
Loop
For Each val As Integer In lst
output.show(val)
Next
output.show("合计:" & lst.Sum)
你可以在命令窗口执行上面的代码