/* 100 dollars exchange in Picat. http://www.decisionsciencenews.com/2017/06/19/counterintuitive-problem-everyone-room-keeps-giving-dollars-random-others-youll-never-guess-happens-next/ """ Imagine a room full of 100 people with 100 dollars each. With every tick of the clock, every person with money gives a dollar to one randomly chosen other person. After some time progresses, how will the money be distributed? """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => N = 100, Init = 100, L = new_array(N), bind_vars(L,Init), _ = random2(), T = 5000, % Max = [], % println(L), while(T > 0) foreach(Ix in [I : I in 1..N, L[I] > 0]) From := Ix, To = oneof(1..N), L[From] := L[From] - 1, L[To] := L[To] + 1 end, println(sort_down(L)), % Max := Max ++ [max(L)], T := T-1 end, println(result=sort_down(L)), % println(max=Max), nl. % A different problem: % """ % Imagine a room full of 100 people _standing in a circle_ with 100 dollars each. With every tick % of the clock, every person with money gives a dollar to one randomly chosen other _neighbour_. % After some time progresses, how will the money be distributed? % """ % New: Randomly choosen neighbour. % Note: It don't seems to change the distribution much. go2 => N = 100, Init = 100, L = new_array(N), bind_vars(L,Init), _ = random2(), T = 5000, % Max = [], % println(L), while(T > 0) foreach(Ix in [I : I in 1..N, L[I] > 0]) From := Ix, To = oneof([Ix-1,Ix+1]), if To == 0 then To := N elseif To == N+1 then To := 1 end, L[From] := L[From] - 1, L[To] := L[To] + 1 end, println(sort_down(L)), % Max := Max ++ [max(L)], T := T-1 end, println(result=sort_down(L)), % println(max=Max), nl. oneof(L) = L[1+(random() mod L.length)].