/* Breaking stick in Picat. https://brainstellar.com/puzzles/probability/119 """ The stick drops and breaks at a random point distributed uniformly across the length. What is the expected length of the smaller part? """ Cf my Gamble model gamble_breaking_stick.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. /* Using sticks with length 1: var : smallest Probabilities (truncated): 0.499999668330164: 0.0001000000000000 0.49999303125161: 0.0001000000000000 0.49995817817713: 0.0001000000000000 0.499955813619885: 0.0001000000000000 ......... 0.000066352811305: 0.0001000000000000 0.000062417797997: 0.0001000000000000 0.000061309556395: 0.0001000000000000 0.000027947769181: 0.0001000000000000 mean = 0.249902 */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => Part1 = beta_dist(1,1), Part2 = 1- Part1, Smallest = cond(Part1 < Part2, Part1,Part2), add("smallest",Smallest). /* Using integers (1..100) instead. Same behaviour: var : smallest Probabilities (truncated): 34: 0.0240000000000000 49: 0.0232000000000000 21: 0.0220000000000000 15: 0.0218000000000000 ......... 29: 0.0170000000000000 11: 0.0160000000000000 50: 0.0103000000000000 0: 0.0088000000000000 mean = 25.0015 */ go2 ?=> reset_store, run_model(10_000,$model2,[show_probs_trunc,mean]), nl, % show_store_lengths,nl, % fail, nl. go2 => true. model2() => N = 100, Part1 = random_integer1(N), Part2 = N - Part1, Smallest = cond(Part1 < Part2, Part1,Part2), add("smallest",Smallest).