/* Second chance in Picat. https://brainstellar.com/puzzles/probability/114 """ Roll a die, and you get paid what the dice shows. But if you want, you can request a second chance & roll the die again; get paid what the second roll shows instead of the first. What is the expected value? """ Cf my Gamble model gamble_second_chance.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. /* If we roll again if first roll is below or equal a limit then the best limit is 3 with an expected outcome of 17/4 (4.25) limit = 1 var : roll Probabilities: 3: 0.1999000000000000 6: 0.1972000000000000 4: 0.1957000000000000 2: 0.1910000000000000 5: 0.1859000000000000 1: 0.0303000000000000 mean = 3.9075 limit = 2 var : roll Probabilities: 5: 0.2235000000000000 6: 0.2232000000000000 4: 0.2216000000000000 3: 0.2182000000000000 1: 0.0590000000000000 2: 0.0545000000000000 mean = 4.1657 limit = 3 var : roll Probabilities: 6: 0.2561000000000000 4: 0.2518000000000000 5: 0.2400000000000000 3: 0.0866000000000000 1: 0.0828000000000000 2: 0.0827000000000000 mean = 4.2518 limit = 4 var : roll Probabilities: 5: 0.2768000000000000 6: 0.2752000000000000 4: 0.1162000000000000 3: 0.1121000000000000 1: 0.1107000000000000 2: 0.1090000000000000 mean = 4.165 limit = 5 var : roll Probabilities: 6: 0.3052000000000000 1: 0.1430000000000000 5: 0.1400000000000000 3: 0.1389000000000000 2: 0.1370000000000000 4: 0.1359000000000000 mean = 3.9085 limit = 6 var : roll Probabilities: 3: 0.1708000000000000 6: 0.1683000000000000 4: 0.1673000000000000 1: 0.1649000000000000 5: 0.1644000000000000 2: 0.1643000000000000 mean = 3.5069 */ go ?=> member(Limit,1..6), println(limit=Limit), reset_store, run_model(10_000,$model(Limit),[show_probs_trunc,mean]), nl, % show_store_lengths,nl, fail, nl. go => true. model(Limit) => N = 6, RollDice = random_integer1_n(N,2), Roll1 = RollDice[1], Roll = cond(Roll1 <= Limit, RollDice[2], Roll1), add("roll",Roll). /* Decide to roll again by chance (50-50). var : roll Probabilities: 6: 0.1730000000000000 5: 0.1674000000000000 1: 0.1669000000000000 4: 0.1658000000000000 3: 0.1650000000000000 2: 0.1619000000000000 mean = 3.5239 */ go2 ?=> reset_store, run_model(10_000,$model2,[show_probs_trunc,mean]), nl, % show_store_lengths,nl, fail, nl. go2 => true. model2() => N = 6, RollDice = random_integer1_n(N,2), Roll1 = RollDice[1], Roll = condt(flip(1/2), RollDice[2], Roll1), add("roll",Roll).