/* Drunk man and keys problem in Picat. https://medium.com/recreational-maths/drunk-man-and-keys-problem-37df70a0cf15 """ The drunk man and keys problem is a simple but interesting probability problem. A drunk man, returning home, is trying to open his house door with a bunch of keys. There are 10 keys in the bunch, but only 1 fits the door. He selects a key at random. If it fits the door he lets himself in, but if it doesn’t fit he tries again. Because he is drunk, he doesn’t remember which keys he has already tried, so he makes a totally random choice from the 10 keys each time. The question is, on which attempt is he most likely to get in? Some people think that the answer is 5 (because that is half of 10). Others say that there is no right answer because every time he tries he has exactly the same chance of picking the right key. These might seem like reasonable answers, but they are answers to the wrong question. In fact, he is most likely to get in on his first attempt! """ Perhaps length 1 (i.e. success at the first trial) is "most likely", but it's not by much over - say - length 2. But, yes, 1 is the most likely. Also, the mean is about 10. This is a port of my Gamble model gamble_drunk_man_and_keys_problem.rkt. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import ppl_distributions,ppl_utils. main => go. /* var : len Probabilities: 1: 0.10093669788992966 (3028 / 29999) 2: 0.08816960565352179 (2645 / 29999) 3: 0.07900263342111404 (2370 / 29999) 4: 0.07276909230307677 (2183 / 29999) 5: 0.06330211007033568 (1899 / 29999) 6: 0.05950198339944665 (1785 / 29999) 7: 0.05463515450515017 (1639 / 29999) 8: 0.04706823560785359 (1412 / 29999) 9: 0.04370145671522384 (1311 / 29999) 10: 0.03813460448681623 (1144 / 29999) 11: 0.03676789226307543 (1103 / 29999) 12: 0.03276775892529751 (983 / 29999) 13: 0.02860095336511217 (858 / 29999) 14: 0.02413413780459349 (724 / 29999) 15: 0.02273409113637121 (682 / 29999) 16: 0.02083402780092670 (625 / 29999) 18: 0.01793393113103770 (538 / 29999) 17: 0.01760058668622288 (528 / 29999) 19: 0.01523384112803760 (457 / 29999) 20: 0.01266708890296343 (380 / 29999) 21: 0.01216707223574119 (365 / 29999) 22: 0.01110037001233375 (333 / 29999) 23: 0.01006700223340778 (302 / 29999) 24: 0.00913363778792626 (274 / 29999) 25: 0.00830027667588920 (249 / 29999) 26: 0.00736691223040768 (221 / 29999) 27: 0.00646688222940765 (194 / 29999) 29: 0.00543351445048168 (163 / 29999) 30: 0.00540018000600020 (162 / 29999) 28: 0.00503350111670389 (151 / 29999) 32: 0.00406680222674089 (122 / 29999) 33: 0.00373345778192606 (112 / 29999) 36: 0.00323344111470382 (97 / 29999) 34: 0.00323344111470382 (97 / 29999) 31: 0.00296676555885196 (89 / 29999) 35: 0.00273342444748158 (82 / 29999) 38: 0.00236674555818527 (71 / 29999) 37: 0.00223340778025934 (67 / 29999) 39: 0.00210007000233341 (63 / 29999) 40: 0.00183339444648155 (55 / 29999) 41: 0.00153338444614821 (46 / 29999) 42: 0.00143338111270376 (43 / 29999) 44: 0.00123337444581486 (37 / 29999) 43: 0.00100003333444448 (30 / 29999) 46: 0.00096669888996300 (29 / 29999) 45: 0.00083336111203707 (25 / 29999) 49: 0.00076669222307410 (23 / 29999) 48: 0.00076669222307410 (23 / 29999) 47: 0.00073335777859262 (22 / 29999) 50: 0.00066668888962965 (20 / 29999) 51: 0.00056668555618521 (17 / 29999) 53: 0.00046668222274076 (14 / 29999) 52: 0.00040001333377779 (12 / 29999) 55: 0.00030001000033334 (9 / 29999) 54: 0.00030001000033334 (9 / 29999) 59: 0.00023334111137038 (7 / 29999) 66: 0.00020000666688890 (6 / 29999) 64: 0.00020000666688890 (6 / 29999) 57: 0.00020000666688890 (6 / 29999) 69: 0.00016667222240741 (5 / 29999) 62: 0.00016667222240741 (5 / 29999) 61: 0.00016667222240741 (5 / 29999) 56: 0.00016667222240741 (5 / 29999) 78: 0.00013333777792593 (4 / 29999) 60: 0.00013333777792593 (4 / 29999) 67: 0.00010000333344445 (3 / 29999) 65: 0.00010000333344445 (3 / 29999) 81: 0.00006666888896297 (2 / 29999) 70: 0.00006666888896297 (2 / 29999) 68: 0.00006666888896297 (2 / 29999) 58: 0.00006666888896297 (2 / 29999) 98: 0.00003333444448148 (1 / 29999) 96: 0.00003333444448148 (1 / 29999) 93: 0.00003333444448148 (1 / 29999) 89: 0.00003333444448148 (1 / 29999) 82: 0.00003333444448148 (1 / 29999) 80: 0.00003333444448148 (1 / 29999) 79: 0.00003333444448148 (1 / 29999) 76: 0.00003333444448148 (1 / 29999) 73: 0.00003333444448148 (1 / 29999) 63: 0.00003333444448148 (1 / 29999) mean = 10.0606 */ go ?=> reset_store, run_model(10_000,$model,[show_probs_rat,mean]), nl. go => true. f(I,A,N,MaxTries) = Res => if I > MaxTries then Res = [A,false] else T = random_integer(N), if T == 0 then Res = [A ++ [T],true] else Res = f(I+1, A++[T],N,MaxTries) end end. model() => N = 10, MaxTries = 100, Res = f(0,[],N,MaxTries), A = Res.first, Success = Res.second, Len = A.len, P = cond((Success, A.last == 0),true,false), observe(P), if observed_ok then add("len",Len) end.