51 lines
1.1 KiB
Text
51 lines
1.1 KiB
Text
% This program needs to be merged with PCLU's "misc" library
|
|
% to use the random number generator.
|
|
|
|
experiment = cluster is run
|
|
rep = null
|
|
|
|
own awake: int := 0
|
|
own awake_heads: int := 0
|
|
|
|
% Returns true if heads, false if tails
|
|
coin_toss = proc () returns (bool)
|
|
return(random$next(2)=1)
|
|
end coin_toss
|
|
|
|
% Do the experiment once
|
|
do_experiment = proc ()
|
|
heads: bool := coin_toss()
|
|
|
|
% monday - wake up
|
|
awake := awake + 1
|
|
if heads then
|
|
awake_heads := awake_heads + 1
|
|
return
|
|
end
|
|
|
|
% tuesday - wake up if tails
|
|
awake := awake + 1
|
|
end do_experiment
|
|
|
|
% Run the experiment N times
|
|
run = proc (n: int) returns (real)
|
|
awake := 0
|
|
awake_heads := 0
|
|
|
|
for i: int in int$from_to(1,n) do
|
|
do_experiment()
|
|
end
|
|
|
|
return(real$i2r(awake_heads) / real$i2r(awake))
|
|
end run
|
|
end experiment
|
|
|
|
start_up = proc ()
|
|
N = 1000000
|
|
|
|
po: stream := stream$primary_output()
|
|
stream$puts(po, "Chance of waking up with heads: ")
|
|
|
|
chance: real := experiment$run(N)
|
|
stream$putl(po, f_form(chance, 1, 6))
|
|
end start_up
|