View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Sum of 15 random +/- integers always greater than or equal to zero

Hi Mike,

If (?) this is right there's is a very small possibility the Do loop could
run many times

Sub test()
Dim i As Long, n As Long
Dim nArr(1 To 15, 1 To 1) As Long ' 2D to dump to cells maybe
Dim nTmpSum As Long, nSum As Long
Const cMinMaX As Long = 999999 * 2

Dim testCounter As Long

Do
For i = 1 To 15
nArr(i, 1) = cMinMaX * (Rnd() - 0.5)
nTmpSum = nTmpSum + nArr(i, 1)
Next
nSum = nTmpSum
nTmpSum = 0
testCounter = testCounter + 1
Loop Until nSum = 0

Debug.Print nSum, testCounter & " Do-loop(s)"
'Range("A1:A15").Value = nArr

End Sub

Regards,
Peter T


"MikeM" <michael[dot]mittelmanl[at]db[dot]com wrote in message
...
Howdy:
What I'd like to do is the following:
Generate a column of 15 random integers from -99,999 to +99,999
The sum of these 15 random integers must be greater than or equal to zero.

Thanks.

Mike