1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php function p($a = "") { echo $a . PHP_EOL; } function generateArray($size = 20) { $a = [ ]; for($i = 0; $i < $size; $i ++) { $a [] = 0; } return $a; } function probabilityOf($d) { $random100 = lcg_value () * 100; return ($random100 < $d); } function challenge($percent, $times) { for($i = 0; $i < $times; $i ++) { if (probabilityOf ( $percent )) { return true; } } return false; } function challengeWinAverage($percent, $times, $challengetimes) { $cnt = 0; for($i = 0; $i < $challengetimes; $i ++) { if (challenge ( $percent, $times )) { $cnt ++; } } $ave = $cnt * 100.0 / $challengetimes; return $ave; } function calcAnalyticalSolution($percent, $times) { return (100 * (1 - (pow ( 1 - $percent / 100.0, $times )))); } function main() { $percent = 10; $times = 16; $challengetimes = 100000; p ( $percent . "%の確率が" . $times . "回の試行の中で1回以上発生する確率" ); $wa = challengeWinAverage ( $percent, $times, $challengetimes ); p ( "計算解 : " . $wa . "%" ); $as = calcAnalyticalSolution ( $percent, $times ); p ( "解析解 : " . $as . "%" ); } main (); |
1 2 3 |
10%の確率が16回の試行の中で1回以上発生する確率 計算解 : 81.549% 解析解 : 81.469798111482% |