Imagine that you become a member of a game in which you need to choose one of three doors. Behind one of the doors is a car, behind two other doors - a goat. You choose one of the doors, for example, number 1, then the presenter, who knows where the car is and where the goats open, opens one of the remaining doors, for example, number 3, behind which there is a goat. After that, he asks you if you would like to change your choice and choose door number 2. Will your chances of winning a car increase if you accept the offer of the presenter and change your choice?
<?php $wins=0; $looses=0; $change_choice = 1; // for ($i=1;$i<=10000;$i++){ $car_position = rand(1,3); #Let's begin #Step 1 $gamer_first_choice = rand(1,3); $showman_choice = rand(1,3); while($showman_choice==$gamer_first_choice || $showman_choice==$car_position){ $showman_choice=rand(1,3); } #echo "Car is in ".$car_position." door. Gamer choose ".$gamer_first_choice." door, showman opens ".$showman_choice." door. Continue..."; #Step 2 if ($change_choice!=0){ for($second=1;$second<=3;$second++){ if($second!=$gamer_first_choice && $second!=$showman_choice){ $gamer_second_choice = $second; // } } } #echo "Gamer's first choice is ".$gamer_first_choice.". Showman opens ".$showman_choice." door. Gamer changes choice to ".$gamer_second_choice."."; if ($gamer_second_choice==$car_position) { #echo 'Gamer won!'; $wins++; } else { #echo 'Gamer loose.'; $looses++; } } echo "Number of games: ".$i.". Gamer wins ".$wins." times. Gamer looses ".$looses." times."; ?>
<?php $wins=0; $looses=0; $change_choice = 1; // for ($i=1;$i<=10000;$i++){ $car_position = rand(1,3); #Let's begin #Step 1 $gamer_first_choice = rand(1,3); $showman_choice = rand(1,3); while($showman_choice==$gamer_first_choice || $showman_choice==$car_position){ $showman_choice=rand(1,3); } #echo "Car is in ".$car_position." door. Gamer choose ".$gamer_first_choice." door, showman opens ".$showman_choice." door. Continue..."; #Step 2 if ($change_choice!=0){ for($second=1;$second<=3;$second++){ if($second!=$gamer_first_choice && $second!=$showman_choice){ $gamer_second_choice = $second; // } } } #echo "Gamer's first choice is ".$gamer_first_choice.". Showman opens ".$showman_choice." door. Gamer changes choice to ".$gamer_second_choice."."; if ($gamer_second_choice==$car_position) { #echo 'Gamer won!'; $wins++; } else { #echo 'Gamer loose.'; $looses++; } } echo "Number of games: ".$i.". Gamer wins ".$wins." times. Gamer looses ".$looses." times."; ?>
#!/usr/bin/perl use strict; use warnings; my $iteration = 10000; my $change_choice = 1; print "\n"; print modelingMontyHall($change_choice); print "\n"; sub modelingMontyHall { my $change_choice = shift; my $success_counter = 0; my ( $i, $j ); my @doors; # 0 - animal, 1 - car my $gamer_choice; my $showman_open_door; my $result = 0; my $success_door; if ($change_choice) { print "Gamer change choice\n" } else { print "Gamer NOT change choice\n" }; for ( $i = 0 ; $i < $iteration ; $i++ ) { # Preparing @doors = ( 0, 0, 0 ); $success_door = int( rand() * 3 ); $doors[$success_door] = 1; # door with car # First step $gamer_choice = 0; for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $doors[$j] == 0 ) ) { $showman_open_door = $j; last; } } # Second step if ($change_choice) { for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $j != $showman_open_door ) ) { $gamer_choice = $j; last; } } } # Count result if ( $doors[$gamer_choice] ) { $result++; } } return $result; }
#!/usr/bin/perl use strict; use warnings; my $iteration = 10000; my $change_choice = 1; print "\n"; print modelingMontyHall($change_choice); print "\n"; sub modelingMontyHall { my $change_choice = shift; my $success_counter = 0; my ( $i, $j ); my @doors; # 0 - animal, 1 - car my $gamer_choice; my $showman_open_door; my $result = 0; my $success_door; if ($change_choice) { print "Gamer change choice\n" } else { print "Gamer NOT change choice\n" }; for ( $i = 0 ; $i < $iteration ; $i++ ) { # Preparing @doors = ( 0, 0, 0 ); $success_door = int( rand() * 3 ); $doors[$success_door] = 1; # door with car # First step $gamer_choice = 0; for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $doors[$j] == 0 ) ) { $showman_open_door = $j; last; } } # Second step if ($change_choice) { for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $j != $showman_open_door ) ) { $gamer_choice = $j; last; } } } # Count result if ( $doors[$gamer_choice] ) { $result++; } } return $result; }
Source: https://habr.com/ru/post/55321/
All Articles