3v4l.org

run code in 300+ PHP versions simultaneously
<?php $iter = 0; function weight($array) { global $iter; $sum = array_sum($array); $rnd = mt_rand(0,$sum); foreach($array as $k=>$v) { $iter++; if($rnd <= $v) { return $k; } $rnd = $rnd - $v; } } $weights = range(0, 1000000); $values = array_fill(0, count($weights), 0); for ($x = 0; $x < 1000; $x++) { $w = weight($weights); $values[$w] = $values[$w] + 1; } echo "$iter iterations performed"; var_dump($values);
Output for 5.3.0 - 5.3.28, 5.4.0 - 5.4.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/qX9QN on line 20
Process exited with code 255.
Output for 5.2.3 - 5.2.17
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /in/qX9QN on line 20
Process exited with code 255.
Output for 5.2.1 - 5.2.2
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 24 bytes) in /in/qX9QN on line 20
Process exited with code 255.
Output for 5.1.6, 5.2.0

Process exited with code 137.

preferences:
87.28 ms | 401 KiB | 82 Q