Re: Sum of 15 random +/- integers always greater than or equal to
- From: "Gary Keramidas" <GKeramidasATmsn.com>
- Date: Fri, 29 Jun 2007 15:45:47 -0400
tom:
what is v1?
v1(v(i)) = v1(v(i)) + 1
--
Gary
"Tom Ogilvy" <TomOgilvy@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:89B1812D-9BBE-4241-94C9-5913BFA8D681@xxxxxxxxxxxxxxxx
As Jerry says, the chance it will be positive is around 50%, so you could
just generate the numbers until they sum to a positive value.
Sub ABCD()
Dim bd As Long
Dim i As Long
bd = 99999
Dim v(1 To 15) As Long
Dim tot As Long
Do
tot = 0
For i = 1 To 15
v(i) = Int(Rnd() * ((bd + 0.5) * 2) - (bd))
v1(v(i)) = v1(v(i)) + 1
tot = tot + v(i)
Next
Loop Until tot >= 0
Range("A1").Resize(15, 1) = Application.Transpose(v)
End Sub
--
Regards,
Tom Ogilvy
"MikeM" wrote:
Hi Jerry:
Thanks for responding.
I suppose I was thinking along the lines of generating the first random
integer in cell B4. Then, the next step would be to generate a random
integer in cell B5. The code would then SUM B4:B5 and ascertain if the
result were >= zero. If yes, it would proceed to the next cell in the range,
B6. If no, it would generate a new random value for B5 and apply the same
test again until it was successful. For each successive random value the SUM
test range would expand to include the new value.
I think that's what you were saying in your choice of constraints:
"- throw it away and draw again until the sum is non-negative"
Hope this helps. And thanks for your interest!
Mike
"Jerry W. Lewis" wrote:
If they were independent identically distributed discrete uniform random
integers, then the probability of a negative sum would be nearly 0.5. How
do
you want to constrain the "randomness" to ensure that the sum is not
negative? Some possibilities if the 15th value produces a negative sum
would
include,
- replace the 15th value with -SUM(fourteenValues)
- throw it away and draw again until the sum is non-negative
...
Note that the 1st option can result in a value larger than 99,999, in which
case the 2nd option would never terminate.
Jerry
"MikeM" wrote:
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
.
- References:
- RE: Sum of 15 random +/- integers always greater than or equal to
- From: Tom Ogilvy
- RE: Sum of 15 random +/- integers always greater than or equal to
- Prev by Date: Re: Sum of 15 random +/- integers always greater than or equal to zero
- Next by Date: Re: Using msxml2.xmlhttp to automate downloads
- Previous by thread: RE: Sum of 15 random +/- integers always greater than or equal to
- Next by thread: RE: Sum of 15 random +/- integers always greater than or equal to
- Index(es):
Relevant Pages
|