精度がよさそう。今度からこれを使おう。
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 |
<?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 main() { $times = 10000000; $cnt = 0; $percent = 34.56; for($i = 0; $i < $times; $i ++) { if (probabilityOf ( $percent )) { $cnt ++; } } $proportion = $cnt * 100.0 / $times; p ( "cnt : " . $cnt ); p ( "proportion : " . $proportion ); } main (); |
1 2 |
cnt : 3456685 proportion : 34.56685 |