Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 12, 2015 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/d26d1000a9665e8525aa to your computer and use it in GitHub Desktop.
Save trepmal/d26d1000a9665e8525aa to your computer and use it in GitHub Desktop.
<?php
// testing how often a number in a given range will appear over many iterations
$max = 10; // how big of a range? ( 1 thru $max )
$iterations = 100000; // how many iterations
// set up an array for keeping tally
$totals = array_fill( 1, $max, 0 );
for ( $i = 1; $i <= $iterations; $i++ ) {
$totals[ rand( 1, $max ) ]++;
}
echo '<pre>';
foreach ( $totals as $num => $count ) {
$percent = $count/$iterations*100;
echo "$num: $percent%\n";
}
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment