in reply to Re^3: Marilyn Vos Savant's Monty Hall problem
in thread Marilyn Vos Savant's Monty Hall problem
#!/usr/bin/perl my $wins = 0; my $loses = 0; print "\n"; for (my $i=1;$i<=1000000;$i++) { #all goats @doors = [0,0,0]; #add a car $doors[int rand(3)] = 1; #contestant choice (cute varname declaration) my $choice = int rand(3); #monty makes his choice my $shown = -1; while($shown == -1) { $shown = int rand(3); if ( $shown == $choice || # he won't show contestants choice $doors[$shown] == 1 # or show the car ) { $shown = -1 } } $doors[$shown] = -1; # out of the equasion #If your on the car now, a change is a loss if($doors[$choice] == 1) { $loses++; } # presuming you don't pick the exposed goat, else is # a win. else { $wins++; } printf("\r Wins:%8d Losses:%8d Win \%:%5.3f", $wins,$loses,($wins/($i))*100); }
|
|---|