Sleeping Beauty problem: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) m (Move C++ to the right alphabetical position) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 199: | Line 199: | ||
Sleeping Beauty should estimate a credence of: 0.333253 |
Sleeping Beauty should estimate a credence of: 0.333253 |
||
</pre> |
</pre> |
||
=={{header|CLU}}== |
|||
<lang clu>% 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 </lang> |
|||
{{out}} |
|||
<pre>Chance of waking up with heads: 0.333758</pre> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |