/* Colored runs of cards in Picat. https://brainstellar.com/puzzles/probability/201 """ There are 26 black(B) and 26 red(R) cards in a standard deck. A run is number of blocks of consecutive cards of the same color. For example, a sequence RRRRBBBRBRB of only 11 cards has 6 runs; namely, RRRR, BBB, R, B, R, B. Find the expected number of runs in a shuffled deck of cards. Answer: 27 """ Cf my Gamble model gamble_colored_runs_of_cards.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 : num_runs Probabilities: 27: 0.1103000000000000 26: 0.1061000000000000 28: 0.1036000000000000 25: 0.0989000000000000 29: 0.0913000000000000 30: 0.0845000000000000 24: 0.0788000000000000 23: 0.0622000000000000 31: 0.0543000000000000 32: 0.0445000000000000 22: 0.0435000000000000 21: 0.0270000000000000 33: 0.0247000000000000 20: 0.0205000000000000 34: 0.0163000000000000 19: 0.0086000000000000 35: 0.0079000000000000 18: 0.0059000000000000 36: 0.0035000000000000 37: 0.0024000000000000 17: 0.0021000000000000 16: 0.0013000000000000 38: 0.0008000000000000 15: 0.0004000000000000 39: 0.0003000000000000 14: 0.0002000000000000 13: 0.0001000000000000 mean = 26.8947 Percentiles: (0.001 16) (0.01 18.990000000000009) (0.025 20) (0.05 21) (0.1 22) (0.25 24) (0.5 27) (0.75 29) (0.84 30) (0.9 32) (0.95 33) (0.975 34) (0.99 35) (0.999 38) (0.9999 39) (0.99999 39) */ go ?=> reset_store, run_model(10_000,$model,[show_probs,mean,show_percentiles]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => N = 26, Cards = shuffle(ones(N,r) ++ ones(N,b)), NumRuns = num_runs(Cards), add("num_runs",NumRuns).