Created
December 6, 2019 16:17
-
-
Save tjlytle/73e0f928ae3ee5f90d09157103f1ee71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$limit = 1000000; | |
$counts = [ | |
'1' => 0, | |
'2' => 0, | |
'3' => 0, | |
'4' => 0, | |
'5' => 0, | |
'6' => 0, | |
'7' => 0, | |
'8' => 0, | |
'9' => 0 | |
]; | |
for ($current = 0; $current < $limit; $current++) { | |
//$number = (string) rand(1, getrandmax()); | |
$number = (string) random_int(1, PHP_INT_MAX); | |
$counts[$number[0]]++; | |
} | |
foreach ($counts as $number => $count) { | |
echo $number . ': ' . ($count/$limit)*100 . '%' . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on what I read, it looks like this implementation is wrong (those are the percentage of occurences), but should look like:
Output
See implementation and expected outputs in other languages: https://rosettacode.org/wiki/Benford%27s_law#JavaScript