44 lines
938 B
Text
44 lines
938 B
Text
program sleeping_beauty;
|
|
$ amount of times to run the experiment
|
|
const N = 1000000;
|
|
|
|
$ seed RNG
|
|
setrandom(0);
|
|
|
|
chance := simulate(N);
|
|
print("Chance of waking up with heads:", chance);
|
|
|
|
$ Run the experiment N times
|
|
proc simulate(amount);
|
|
state := {};
|
|
state.wakings := 0;
|
|
state.heads := 0;
|
|
|
|
loop init i := 0; step i +:= 1; until i = amount do
|
|
run_experiment(state);
|
|
end loop;
|
|
|
|
return state.heads / state.wakings;
|
|
end proc;
|
|
|
|
$ Run the experiment once
|
|
proc run_experiment(rw state);
|
|
heads := coin_toss();
|
|
|
|
$ monday - wake up
|
|
state.wakings +:= 1;
|
|
|
|
if heads then
|
|
state.heads +:= 1;
|
|
return;
|
|
end if;
|
|
|
|
$ tuesday - wake up if tails
|
|
state.wakings +:= 1;
|
|
end proc;
|
|
|
|
$ Toss a coin
|
|
proc coin_toss();
|
|
return 1 = random 1;
|
|
end proc;
|
|
end program;
|