Author Topic: Maximum Consecutive Same Heads/Tails in Coin Toss  (Read 3099 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Maximum Consecutive Same Heads/Tails in Coin Toss
« on: November 24, 2021, 12:41:38 pm »
When I created my coin toss graphics program https://www.qb64.org/forum/index.php?topic=4117.msg134684#msg134684 as a check that it was working correctly, there must not have been many consecutive same heads or tails (the program did seem OK in this respect).

But I wondered if you let QB64 run continuously with a 50/50 random, what would the maximum same consecutive be.  [Theoretically (?) if you do 50/50 for an infinite time you'd get a sequence sometime of infinite consecutive same].

So this simple program examines this.

Code: QB64: [Select]
  1. CONST False = 0, True = NOT False
  2. Count& = 0
  3. Cycle& = 0
  4. SameCount% = 0
  5. MaxSame% = 0
  6. IF RND < 0.5 THEN
  7.     Heads%% = True
  8.     Heads%% = False
  9. OldHeads%% = Heads%%
  10.  
  11.     IF RND < 0.5 THEN
  12.         Heads%% = True
  13.     ELSE
  14.         Heads%% = False
  15.     END IF
  16.     IF Heads%% = OldHeads%% THEN
  17.         SameCount% = SameCount% + 1
  18.         IF SameCount% > MaxSame% THEN MaxSame% = SameCount%
  19.     ELSE
  20.         SameCount% = 0
  21.         'IF Heads%% THEN
  22.         '    OldHeads%% = False
  23.         'ELSE
  24.         '    OldHeads%% = True
  25.         'END IF
  26.         OldHeads%% = NOT OldHeads%%
  27.         'IF RND < 0.5 THEN
  28.         '    Heads%% = True
  29.         '    OldHeads%% = True
  30.         'ELSE
  31.         '    Heads%% = False
  32.         '    OldHeads%% = False
  33.         'END IF
  34.     END IF
  35.     Count& = Count& + 1
  36.     IF Count& / 10000000 = Count& \ 10000000 THEN
  37.         Count& = 0
  38.         PRINT MaxSame%
  39.         RANDOMIZE (TIMER)
  40.     END IF
  41.     Cycle& = Cycle& + 1
  42.     IF Cycle& > 2147483646 THEN END
  43. PRINT Cycle&
  44.  

The program runs freely and occasionally prints the maximum number of same consecutive "heads or tails".  On my laptop this number is 24, seemingly never exceeded.  The likelihood of getting 24 same consecutive heads or tails is low (~ 1 in 16 million, but the program is doing billions of cycles).  Occasionally, one might expect the maximum of 24 to be exceeded.

I think that I must be doing something stupid with the code.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maximum Consecutive Same Heads/Tails in Coin Toss
« Reply #1 on: November 24, 2021, 02:44:01 pm »
Wasting polls usually made it worse but I got it to do 26 with this (2 wasted polls):

Code: QB64: [Select]
  1. lastH = -1
  2.  
  3.  
  4.  
  5.     ' get 24 max run unless I uncomment next
  6.     x = Rnd 'waste a poll
  7.     x = Rnd ' again
  8.     'x = Rnd ' again
  9.     'x = Rnd ' again
  10.  
  11.     If Rnd < 0.5 Then
  12.         H = -1
  13.     Else
  14.         H = 0
  15.     End If
  16.     If H = lastH Then
  17.         same = same + 1
  18.         If same > MaxSame Then MaxSame = same
  19.     Else
  20.         same = 0
  21.         lastH = H
  22.     End If
  23.     C = C + 1
  24.     If C Mod 10000 = 0 Then
  25.         Print MaxSame
  26.         Randomize (Timer + Rnd * 10000)
  27.     End If
  28. Print MaxSame
  29.  

Not every time but often and don't have to wait long:
 
wasting polls.PNG