Skip to content

Instantly share code, notes, and snippets.

@mixdev
Last active April 29, 2017 17:19
Show Gist options
  • Save mixdev/49e9a672571f9309676aabd1c2134565 to your computer and use it in GitHub Desktop.
Save mixdev/49e9a672571f9309676aabd1c2134565 to your computer and use it in GitHub Desktop.
Simple Monte Carlo Simulation of rolling two regular dice. Shows visual results in shell. Shows clear central tendency. The problem stated here http://www.goldsim.com/Web/Introduction/Probabilistic/MonteCarlo/
<?php
$simulations = 10000;
$dicesides = 6;
$count = Array();
for($i=0; $i<$simulations; $i++) {
$x = mt_rand(1,$dicesides);
$y = mt_rand(1,$dicesides);
@$count[$x+$y]++;
}
for($i=2;$i<= 2 * $dicesides;$i++){
$prob = $count[$i]/$simulations;
$size=str_pad('',$prob*200,'=');
echo "P($i)\t=\t". $prob ."\t". $size ."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment