Originally posted by benkillin
I think it's time for me to share some code again:
Coin flip simulator:
just some optimizations ideas, nothing major
<?php
$tf = array('T','H');
$results = array(); //T = dubTails H=dubHeads and E=oneEach
for($i=0; $i<100; $i++){// i seem to recall that <= is more intensive
$curFlip1 = $tf[rand(0,1)];
$curFlip2 = $tf[rand(0,1)];
if($curFlip1 == $curFlip2) //why check twice?
$results[$curFlip1]++; //take advantage of the increment operators and flip values to indice the results array
else
$results['E']++; //1 each
}
echo 'Double Tails: '.$results['T'].'<br /> \r\n Heads and Tails: '.$results['E'].'<br /> \r\n Double Heads: '.$results['H'];
?>
just something I noticed could be improved a bit