three-doors.html


001| """Three Doors - The Monty Hall Puzzle.
001| 
001| Doors: [1]=33,331, [2]=33,394, [3]=33,275; tries=100,000
001| tries: 100,000, wins: 66,729 (66.729000 %)
001| 
001| Doors: [1]=33,403, [2]=33,375, [3]=33,222; tries=100,000
001| tries: 100,000, wins: 33,488 (33.488000 %)
001| 
001| """

002| ####################################################################################################
003| from sys import argv
004| from random import randint, sample
005| ####################################################################################################
006| BasePath = r'C:\Demo\HCC\Python'
007| 
008| 
009| ##================================================================================================##
010| def three_doors (switch_doors=True, n_times=100_000, verbose=False):
011|     '''The Monty Hall 3 Doors Problem.'''
012| 
013|     wins = 0
014|     goat = 'goat'
015|     car = 'car'
016|     tries = []
017| 
018|     # Generate a set of random three-door problems...
019|     for n in range(n_times):
020|         # Start with goats behind all doors...
021|         doors = [goat, goat, goat]
022| 
023|         # Change a random one to a car...
024|         doors[randint(0,2)] = 'car'
025| 
026|         # Add to the list...
027|         tries.append(doors)
028| 
029|     # Generate some distribution stats to see if we're random...
030|     fx = lambda x: (1 if x=='car' else 0)
031|     n0 = sum(fx(t[0]) for t in tries)
032|     n1 = sum(fx(t[1]) for t in tries)
033|     n2 = sum(fx(t[2]) for t in tries)
034| 
035|     print(f'Doors: [1]={n0:,}, [2]={n1:,}, [3]={n2:,}; tries={len(tries):,}')
036|     if verbose:
037|         print()
038| 
039|     # Go through each try...
040|     for tx,t in enumerate(tries):
041|         # Guess at a door...
042|         dpick = randint(0,2)
043| 
044|         # Host considers other doors with goats...
045|         others = [(dx,d) for dx,d in enumerate(t) if d != 'car' if dx != dpick]
046| 
047|         # Host randomly opens a door with a goat...
048|         dopen = sample(others, k=1)[0]
049| 
050|         # If we're switching doors...
051|         if switch_doors:
052|             # Pick the remaining door...
053|             dswap = 3 - (dpick + dopen[0])
054| 
055|             # If the car was behind it, we won!...
056|             if t[dswap] == 'car':
057|                 wins += 1
058| 
059|             if verbose:
060|                 spick = f'[{dpick}]({t[dpick]:4s})'
061|                 sopen = f'[{dopen[0]}]{dopen[1]}'
062|                 sswap = f'[{dswap}]{t[dswap]:4s}'
063|                 print(f'{spick} vs {sopen} => {sswap}')
064| 
065|         # If we're staying with the door we picked...
066|         else:
067|             # If the car was behind it, we won!...
068|             if t[dpick] == 'car':
069|                 wins += 1
070| 
071|             if verbose:
072|                 spick = f'[{dpick}]{t[dpick]:4s}'
073|                 sopen = f'[{dopen[0]}]{dopen[1]}'
074|                 print(f'{spick} vs {sopen}')
075| 
076|     if verbose:
077|         print()
078|     print(f'tries: {tx+1:,}, wins: {wins:,} ({100*(wins/(tx+1)):2f} %)')
079|     print()
080| 
081| 
082| ##================================================================================================##
083| def main (*args):
084|     three_doors(switch_doors=True , n_times=100_000, verbose=False)
085|     three_doors(switch_doors=False, n_times=100_000, verbose=False)
086|     return ()
087| ####################################################################################################
088| if __name__ == '__main__':
089|     print(argv[0])
090|     print()
091|     rv = main(*argv[1:])
092|     print()
093| ####################################################################################################
094| '''eof'''
095|