I want to make a multiple of four in a curious way: First I choose a random integer 1 to 4, each equally likely. If I get the '4', I stop immediately and that is my number. As long as I do not have a multiple of four, I continue to choose more random integers 1 to 4, keeping track of the running total. When the running total is a multiple of four, then I stop.
What is the expected number of random integers I need to get my multiple of four and what is the expected value of the running total?
Variation: The first random integer is the same, but the second and subsequent integers are equal to, one less, or one more than the previous integer. (If the previous random integer was 3, then I would choose one of 2, 3, or 4.)
In this case what is the expected number of integers needed and what is the expected total?
Assuming that after the first random number is chosen, subsequent integers can be negative or greater than 4 if it so happens, the following program simulates a million trials, repeated ten times:
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) & Chr(10)
For repetition = 1 To 10
Randomize Timer
sumtot = 0: summvs = 0
For trial = 1 To 1000000
r = Int(Rnd(1) * 4 + 1)
tot = r: mvs = 1
Do
If tot Mod 4 = 0 Then Exit Do
r = r + Int(3 * Rnd(1)  1)
tot = tot + r: mvs = mvs + 1
Loop
sumtot = sumtot + tot: summvs = summvs + mvs
Next trial
trials = trial  1
avgtot = sumtot / trials: avgmvs = summvs / trials
Form1.Visible = True
Text1.Text = Text1.Text & Str(avgtot) & Str(avgmvs) & crlf$
DoEvents
Next repetition
End Sub
does 10 repetitions of a million trials each.
It finds:
average
total number of
random numbers
8.497256 4.001558
8.492068 3.998804
8.507884 3.999169
8.481284 3.996068
8.506332 3.999093
8.493396 3.997729
8.496948 3.997438
8.489952 3.996578
8.495616 4.000671
8.497752 4.000906
It would seem that 4 would be the average number of randoms chosen and 8.5 would be the average total of numbers chosen, remembering that sometimes there are negative numbers causing the total to go down. The numbers seem to be skewed on the low side ( on the order of .001 or so), but this might be an artifact of an imperfect random number generator repeating same sequences of values over and over somewhat.

Posted by Charlie
on 20140430 14:38:53 