/* Innocent monkey in Picat. https://brainstellar.com/puzzles/probability/115 """ A very innocent monkey throws a fair die. The monkey will eat as many bananas as are shown on the die, from 1 to 5. But if the die shows '6', the monkey will eat 5 bananas and throw the die again. This may continue indefinitely. What is the expected number of bananas the monkey will eat? Answer: 4 """ Cf my Gamble model gamble_innocent_monkey.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. import util. main => go. /* var : len Probabilities: 1: 0.8342000000000001 2: 0.1377000000000000 3: 0.0238000000000000 4: 0.0039000000000000 5: 0.0004000000000000 mean = 1.1986 var : total Probabilities (truncated): 2: 0.1689000000000000 4: 0.1687000000000000 5: 0.1680000000000000 3: 0.1647000000000000 ......... 16: 0.0006000000000000 24: 0.0002000000000000 25: 0.0001000000000000 23: 0.0001000000000000 mean = 4.0054 */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean]), nl, % show_store_lengths,nl, % fail, nl. go => true. f(A,N) = Res => R = random_integer1(N), if R <= 5 then Res = A ++ [R] else Res = f(A ++ [5],N) end. model() => N = 6, A = f([],N), Len = A.len, Total = A.sum, add("len",Len), add("total",Total).