Sleeping Beauty problem: Difference between revisions

Content added Content deleted
(Added Perl)
Line 334: Line 334:
<pre>Wakings over 1000000 repetitions = 1499741
<pre>Wakings over 1000000 repetitions = 1499741
Percentage probability of heads on waking = 33.35636%</pre>
Percentage probability of heads on waking = 33.35636%</pre>

=={{header|Perl}}==
<lang perl>use strict;
use warnings;

sub sleeping_beauty {
my($trials) = @_;
my($gotheadsonwaking,$wakenings);
$wakenings++ and rand > .5 ? $gotheadsonwaking++ : $wakenings++ for 1..$trials;
$wakenings, $gotheadsonwaking/$wakenings
}

printf "Wakenings over $trials experiments: %d\nSleeping Beauty should estimate a credence of: %.4f\n", sleeping_beauty(1_000_000);</lang>
{{out}}
<pre>Wakenings over 1000000 experiments: 1499816
Sleeping Beauty should estimate a credence of: 0.333</pre>

=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<lang Phix>(phixonline)-->