use strict; use warnings; use List::Util qw(min max); sub chose { my $x=shift; $x>0?1-chose(-$x):0.5*exp($x) } my ($a,$b,$n) = @ARGV; # provide numbers and repetitions on command line my $ctr = $n; my $correct = 0; while($ctr--){ my $envelope = rand()<0.5?$a:$b; # pick an envelope at random my $answer = rand()<1-chose($envelope)?'low':'high'; $correct++ # count the correct answers if ( $envelope==min($a,$b) and $answer eq 'low' ) or ( $envelope==max($a,$b) and $answer eq 'high' ); } printf"Correct ones: %20.17f%%, theoretical odds: %20.17f%%\n", 100*$correct/$n, 50+50*(chose(max($a,$b))-chose(min($a,$b)));