Monty Hall problem: Difference between revisions
Content added Content deleted
(Added Elixir) |
(→{{header|Perl 6}}: modernize) |
||
Line 2,433: | Line 2,433: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{works with|rakudo|2015-09-24}} |
|||
This implementation is parametric over the number of doors. [[wp:Monty_Hall_problem#Increasing_the_number_of_doors|Increasing the number of doors in play makes the superiority of the switch strategy even more obvious]]. |
This implementation is parametric over the number of doors. [[wp:Monty_Hall_problem#Increasing_the_number_of_doors|Increasing the number of doors in play makes the superiority of the switch strategy even more obvious]]. |
||
<lang perl6>enum Prize <Car Goat>; |
<lang perl6>enum Prize <Car Goat>; |
||
enum Strategy <Stay Switch>; |
enum Strategy <Stay Switch>; |
||
sub play (Strategy $strategy, Int :$doors = 3) returns Prize { |
sub play (Strategy $strategy, Int :$doors = 3) returns Prize { |
||
# Call the door with a car behind it door 0. Number the |
# Call the door with a car behind it door 0. Number the |
||
# remaining doors starting from 1. |
# remaining doors starting from 1. |
||
my Prize @doors = Car, Goat xx $doors - 1; |
my Prize @doors = flat Car, Goat xx $doors - 1; |
||
# The player chooses a door. |
# The player chooses a door. |
||
my Prize $initial_pick = @doors.splice(@doors.keys.pick,1)[0]; |
my Prize $initial_pick = @doors.splice(@doors.keys.pick,1)[0]; |
||
# Of the n doors remaining, the host chooses n - 1 that have |
# Of the n doors remaining, the host chooses n - 1 that have |
||
# goats behind them and opens them, removing them from play. |
# goats behind them and opens them, removing them from play. |
||
@doors |
while @doors > 1 { |
||
@doors.splice($_,1) |
|||
for pick @doors.elems - 1, grep { @doors[$_] == Goat }, keys @doors; |
|||
when Goat |
|||
given @doors.keys.pick; |
|||
} |
|||
# If the player stays, they get their initial pick. Otherwise, |
# If the player stays, they get their initial pick. Otherwise, |
||
# they get whatever's behind the remaining door. |
# they get whatever's behind the remaining door. |
||
return $strategy === Stay ?? $initial_pick !! @doors[0]; |
return $strategy === Stay ?? $initial_pick !! @doors[0]; |
||
} |
} |
||
constant TRIALS = 1000; |
constant TRIALS = 1000; |
||
for 3, 10 -> $doors { |
for 3, 10 -> $doors { |
||
my %wins; |
my %wins; |