fork(1) download
  1. <?php
  2.  
  3. function MyRound($A,$B){
  4. return floor($A* ($B + rand(0, 9) ) / 100);
  5. }
  6. function CrockerRound($a, $b) {
  7. $d = $a * $b / 100.0;
  8. $f = floor($d);
  9. if (rand(0, 1) > ($d - $f)) return $f + 1.0;
  10. return $f;
  11. }
  12. function SectusRound($A,$B){
  13. return rand(0,1)?floor($A/100*$B):ceil($A/100*$B);
  14. }
  15. function mvwRound($A,$B){
  16. $X = $A * $B / 100;
  17. $Y = (rand(0,1) > 0) ? floor($X) : ceil($X);
  18. return $Y;
  19. }
  20. $solutions=array("MyRound","CrockerRound","SectusRound","mvwRound");
  21. $a=10;
  22. $b=33;
  23. foreach($solutions as $solution){
  24. $c=0;
  25. $d=0;
  26. $e=0;
  27. for($i=0;$i<100000;$i++){
  28. $e=$solution($a,$b);
  29. if($e==3)
  30. $c++;
  31. else if($e==4)
  32. $d++;
  33. }
  34. echo "Solution: $solution\n";
  35. echo "Got 3:$c times\n";
  36. echo "Got 4:$d times\n";
  37. }
Success #stdin #stdout 0.56s 24448KB
stdin
Standard input is empty
stdout
Solution: MyRound
Got 3:70250 times
Got 4:29750 times
Solution: CrockerRound
Got 3:49914 times
Got 4:50086 times
Solution: SectusRound
Got 3:49557 times
Got 4:50443 times
Solution: mvwRound
Got 3:49778 times
Got 4:50222 times