3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $teams[0] = array(1784, 1602, 1550, 1442, 1666); $teams[1] = array(1560, 1758, 1660, 1500, 1440); $diff = array_sum($teams[0]) - array_sum($teams[1]); for ($i = 0; $i < 1000 && $diff != 0; ++$i) { $r1 = mt_rand(0, 4); // assumes nine players on each team $r2 = mt_rand(0, 4); $new_diff = $diff - ($teams[0][$r1] - $teams[1][$r2]) * 2; if (abs($new_diff) < abs($diff) || mt_rand(0, 100) < 5) { // if the switch makes the teams more equal, then swap $tmp = $teams[0][$r1]; $teams[0][$r1] = $teams[1][$r2]; $teams[1][$r2] = $tmp; #var_dump(abs($new_diff)); $diff = $new_diff; } }
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0