3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * @param array $items * @param array $weights * * @return mixed */ function random_weighted(array $items, array $weights) { $weight = 0; $return = current($items); for ($i = 0, $n = count($items); $i < $n; $i++) { $random = mt_rand(0, $weights[$i] + $weight); if ($random >= $weight) { $return = $items[$i]; } $weight += $weights[$i]; } return $return; } $items = ['a', 'b', 'c', 'd']; $weights = [0, 10, 10, 10]; $i = 100; $results = ['a' => 0, 'b' => 0, 'c' => 0, 'd' => 0]; do { $x = random_weighted($items, $weights); $results[$x]++; } while (--$i); var_dump($results);
based on Wb1ZK
Output for 7.2.0
array(4) { ["a"]=> int(0) ["b"]=> int(39) ["c"]=> int(31) ["d"]=> int(30) }
Output for 7.0.5, 7.1.7
array(4) { ["a"]=> int(0) ["b"]=> int(33) ["c"]=> int(31) ["d"]=> int(36) }
Output for 7.1.6
array(4) { ["a"]=> int(0) ["b"]=> int(35) ["c"]=> int(27) ["d"]=> int(38) }
Output for 7.1.5
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(28) ["d"]=> int(45) }
Output for 7.0.3, 7.1.0
array(4) { ["a"]=> int(0) ["b"]=> int(28) ["c"]=> int(32) ["d"]=> int(40) }
Output for 5.6.14, 7.0.20
array(4) { ["a"]=> int(0) ["b"]=> int(26) ["c"]=> int(37) ["d"]=> int(37) }
Output for 5.6.9, 7.0.6
array(4) { ["a"]=> int(0) ["b"]=> int(37) ["c"]=> int(28) ["d"]=> int(35) }
Output for 7.0.4
array(4) { ["a"]=> int(0) ["b"]=> int(26) ["c"]=> int(38) ["d"]=> int(36) }
Output for 7.0.2
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(38) ["d"]=> int(30) }
Output for 7.0.1
array(4) { ["a"]=> int(0) ["b"]=> int(38) ["c"]=> int(29) ["d"]=> int(33) }
Output for 7.0.0
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(30) ["d"]=> int(45) }
Output for 5.6.21
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(35) ["d"]=> int(38) }
Output for 5.6.20
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(39) ["d"]=> int(32) }
Output for 5.6.19
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(32) ["d"]=> int(43) }
Output for 5.6.18
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(40) ["d"]=> int(33) }
Output for 5.6.17
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(32) ["d"]=> int(41) }
Output for 5.6.16
array(4) { ["a"]=> int(0) ["b"]=> int(23) ["c"]=> int(39) ["d"]=> int(38) }
Output for 5.6.15
array(4) { ["a"]=> int(0) ["b"]=> int(37) ["c"]=> int(22) ["d"]=> int(41) }
Output for 5.6.13
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(38) ["d"]=> int(33) }
Output for 5.4.14, 5.6.12
array(4) { ["a"]=> int(0) ["b"]=> int(33) ["c"]=> int(35) ["d"]=> int(32) }
Output for 5.4.19, 5.6.11
array(4) { ["a"]=> int(0) ["b"]=> int(37) ["c"]=> int(30) ["d"]=> int(33) }
Output for 5.6.10
array(4) { ["a"]=> int(0) ["b"]=> int(35) ["c"]=> int(31) ["d"]=> int(34) }
Output for 5.6.8
array(4) { ["a"]=> int(0) ["b"]=> int(28) ["c"]=> int(29) ["d"]=> int(43) }
Output for 5.5.35
array(4) { ["a"]=> int(0) ["b"]=> int(30) ["c"]=> int(29) ["d"]=> int(41) }
Output for 5.5.34
array(4) { ["a"]=> int(0) ["b"]=> int(33) ["c"]=> int(29) ["d"]=> int(38) }
Output for 5.5.33
array(4) { ["a"]=> int(0) ["b"]=> int(31) ["c"]=> int(37) ["d"]=> int(32) }
Output for 5.5.32
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(31) ["d"]=> int(40) }
Output for 5.5.31
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(37) ["d"]=> int(31) }
Output for 5.5.30
array(4) { ["a"]=> int(0) ["b"]=> int(35) ["c"]=> int(33) ["d"]=> int(32) }
Output for 5.5.29
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(34) ["d"]=> int(34) }
Output for 5.5.28
array(4) { ["a"]=> int(0) ["b"]=> int(34) ["c"]=> int(28) ["d"]=> int(38) }
Output for 5.5.27
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(36) ["d"]=> int(39) }
Output for 5.5.26
array(4) { ["a"]=> int(0) ["b"]=> int(39) ["c"]=> int(33) ["d"]=> int(28) }
Output for 5.5.25
array(4) { ["a"]=> int(0) ["b"]=> int(30) ["c"]=> int(30) ["d"]=> int(40) }
Output for 5.5.24
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(42) ["d"]=> int(29) }
Output for 5.4.45
array(4) { ["a"]=> int(0) ["b"]=> int(28) ["c"]=> int(40) ["d"]=> int(32) }
Output for 5.4.25, 5.4.44
array(4) { ["a"]=> int(0) ["b"]=> int(36) ["c"]=> int(32) ["d"]=> int(32) }
Output for 5.4.23, 5.4.43
array(4) { ["a"]=> int(0) ["b"]=> int(36) ["c"]=> int(34) ["d"]=> int(30) }
Output for 5.4.40, 5.4.42
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(37) ["d"]=> int(38) }
Output for 5.4.41
array(4) { ["a"]=> int(0) ["b"]=> int(31) ["c"]=> int(36) ["d"]=> int(33) }
Output for 5.4.39
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(31) ["d"]=> int(42) }
Output for 5.4.38
array(4) { ["a"]=> int(0) ["b"]=> int(30) ["c"]=> int(34) ["d"]=> int(36) }
Output for 5.4.37
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(37) ["d"]=> int(34) }
Output for 5.4.36
array(4) { ["a"]=> int(0) ["b"]=> int(29) ["c"]=> int(35) ["d"]=> int(36) }
Output for 5.4.35
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(33) ["d"]=> int(35) }
Output for 5.4.34
array(4) { ["a"]=> int(0) ["b"]=> int(43) ["c"]=> int(28) ["d"]=> int(29) }
Output for 5.4.32
array(4) { ["a"]=> int(0) ["b"]=> int(36) ["c"]=> int(35) ["d"]=> int(29) }
Output for 5.4.31
array(4) { ["a"]=> int(0) ["b"]=> int(33) ["c"]=> int(26) ["d"]=> int(41) }
Output for 5.4.30
array(4) { ["a"]=> int(0) ["b"]=> int(35) ["c"]=> int(34) ["d"]=> int(31) }
Output for 5.4.20, 5.4.26, 5.4.29
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(34) ["d"]=> int(39) }
Output for 5.4.28
array(4) { ["a"]=> int(0) ["b"]=> int(24) ["c"]=> int(30) ["d"]=> int(46) }
Output for 5.4.27
array(4) { ["a"]=> int(0) ["b"]=> int(38) ["c"]=> int(34) ["d"]=> int(28) }
Output for 5.4.24
array(4) { ["a"]=> int(0) ["b"]=> int(28) ["c"]=> int(37) ["d"]=> int(35) }
Output for 5.4.22
array(4) { ["a"]=> int(0) ["b"]=> int(22) ["c"]=> int(39) ["d"]=> int(39) }
Output for 5.4.21
array(4) { ["a"]=> int(0) ["b"]=> int(27) ["c"]=> int(36) ["d"]=> int(37) }
Output for 5.4.18
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(31) ["d"]=> int(37) }
Output for 5.4.17
array(4) { ["a"]=> int(0) ["b"]=> int(38) ["c"]=> int(26) ["d"]=> int(36) }
Output for 5.4.16
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(46) ["d"]=> int(29) }
Output for 5.4.15
array(4) { ["a"]=> int(0) ["b"]=> int(24) ["c"]=> int(35) ["d"]=> int(41) }
Output for 5.4.13
array(4) { ["a"]=> int(0) ["b"]=> int(40) ["c"]=> int(30) ["d"]=> int(30) }
Output for 5.4.12
array(4) { ["a"]=> int(0) ["b"]=> int(32) ["c"]=> int(35) ["d"]=> int(33) }
Output for 5.3.22 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/r5g49 on line 26
Process exited with code 255.