Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Monty-Hall-problem/PHP/monty-hall-problem.php
Normal file
27
Task/Monty-Hall-problem/PHP/monty-hall-problem.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
function montyhall($iterations){
|
||||
$switch_win = 0;
|
||||
$stay_win = 0;
|
||||
|
||||
foreach (range(1, $iterations) as $i){
|
||||
$doors = array(0, 0, 0);
|
||||
$doors[array_rand($doors)] = 1;
|
||||
$choice = array_rand($doors);
|
||||
do {
|
||||
$shown = array_rand($doors);
|
||||
} while($shown == $choice || $doors[$shown] == 1);
|
||||
|
||||
$stay_win += $doors[$choice];
|
||||
$switch_win += $doors[3 - $choice - $shown];
|
||||
}
|
||||
|
||||
$stay_percentages = ($stay_win/$iterations)*100;
|
||||
$switch_percentages = ($switch_win/$iterations)*100;
|
||||
|
||||
echo "Iterations: {$iterations} - ";
|
||||
echo "Stayed wins: {$stay_win} ({$stay_percentages}%) - ";
|
||||
echo "Switched wins: {$switch_win} ({$switch_percentages}%)";
|
||||
}
|
||||
|
||||
montyhall(10000);
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue