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, 1, 1, 1]; $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 EnkOV
Output for 7.2.0
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(42) ["d"]=> int(45) }
Output for 7.0.3, 7.0.20, 7.1.7
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(32) ["d"]=> int(50) }
Output for 7.1.6
array(4) { ["a"]=> int(0) ["b"]=> int(22) ["c"]=> int(32) ["d"]=> int(46) }
Output for 7.1.5
array(4) { ["a"]=> int(0) ["b"]=> int(17) ["c"]=> int(31) ["d"]=> int(52) }
Output for 7.1.0
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(27) ["d"]=> int(54) }
Output for 7.0.6
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(38) ["d"]=> int(44) }
Output for 7.0.5
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(36) ["d"]=> int(50) }
Output for 5.6.13, 5.6.18, 7.0.4
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(31) ["d"]=> int(55) }
Output for 7.0.2
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(26) ["d"]=> int(56) }
Output for 7.0.1
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(35) ["d"]=> int(40) }
Output for 5.4.16, 5.4.26, 7.0.0
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(33) ["d"]=> int(54) }
Output for 5.4.24, 5.6.28
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(34) ["d"]=> int(48) }
Output for 5.5.33, 5.6.16, 5.6.21
array(4) { ["a"]=> int(0) ["b"]=> int(21) ["c"]=> int(31) ["d"]=> int(48) }
Output for 5.6.20
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(23) ["d"]=> int(52) }
Output for 5.6.19
array(4) { ["a"]=> int(0) ["b"]=> int(21) ["c"]=> int(32) ["d"]=> int(47) }
Output for 5.6.17
array(4) { ["a"]=> int(0) ["b"]=> int(23) ["c"]=> int(25) ["d"]=> int(52) }
Output for 5.6.15
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(35) ["d"]=> int(46) }
Output for 5.4.15, 5.4.28, 5.4.31, 5.6.14
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(30) ["d"]=> int(50) }
Output for 5.4.38, 5.6.12
array(4) { ["a"]=> int(0) ["b"]=> int(16) ["c"]=> int(36) ["d"]=> int(48) }
Output for 5.6.11
array(4) { ["a"]=> int(0) ["b"]=> int(21) ["c"]=> int(33) ["d"]=> int(46) }
Output for 5.6.10
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(35) ["d"]=> int(51) }
Output for 5.6.9
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(38) ["d"]=> int(48) }
Output for 5.6.8
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(27) ["d"]=> int(53) }
Output for 5.5.35
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(29) ["d"]=> int(51) }
Output for 5.5.34
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(37) ["d"]=> int(50) }
Output for 5.4.23, 5.5.31 - 5.5.32
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(34) ["d"]=> int(51) }
Output for 5.5.30
array(4) { ["a"]=> int(0) ["b"]=> int(8) ["c"]=> int(33) ["d"]=> int(59) }
Output for 5.5.29
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(36) ["d"]=> int(49) }
Output for 5.5.28
array(4) { ["a"]=> int(0) ["b"]=> int(17) ["c"]=> int(32) ["d"]=> int(51) }
Output for 5.5.27
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(28) ["d"]=> int(54) }
Output for 5.5.26
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(33) ["d"]=> int(53) }
Output for 5.5.25
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(41) ["d"]=> int(44) }
Output for 5.5.24
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(32) ["d"]=> int(56) }
Output for 5.4.44 - 5.4.45
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(32) ["d"]=> int(48) }
Output for 5.4.43
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(29) ["d"]=> int(52) }
Output for 5.4.20, 5.4.42
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(35) ["d"]=> int(52) }
Output for 5.4.41
array(4) { ["a"]=> int(0) ["b"]=> int(11) ["c"]=> int(41) ["d"]=> int(48) }
Output for 5.4.36, 5.4.40
array(4) { ["a"]=> int(0) ["b"]=> int(17) ["c"]=> int(34) ["d"]=> int(49) }
Output for 5.4.39
array(4) { ["a"]=> int(0) ["b"]=> int(16) ["c"]=> int(39) ["d"]=> int(45) }
Output for 5.4.37
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(38) ["d"]=> int(50) }
Output for 5.4.35
array(4) { ["a"]=> int(0) ["b"]=> int(17) ["c"]=> int(35) ["d"]=> int(48) }
Output for 5.4.34
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(31) ["d"]=> int(50) }
Output for 5.4.32
array(4) { ["a"]=> int(0) ["b"]=> int(23) ["c"]=> int(21) ["d"]=> int(56) }
Output for 5.4.30
array(4) { ["a"]=> int(0) ["b"]=> int(24) ["c"]=> int(27) ["d"]=> int(49) }
Output for 5.4.29
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(28) ["d"]=> int(57) }
Output for 5.4.27
array(4) { ["a"]=> int(0) ["b"]=> int(10) ["c"]=> int(36) ["d"]=> int(54) }
Output for 5.4.25
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(37) ["d"]=> int(51) }
Output for 5.4.22
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(26) ["d"]=> int(60) }
Output for 5.4.21
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(28) ["d"]=> int(60) }
Output for 5.4.19
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(44) ["d"]=> int(43) }
Output for 5.4.18
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(37) ["d"]=> int(45) }
Output for 5.4.17
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(42) ["d"]=> int(46) }
Output for 5.4.14
array(4) { ["a"]=> int(0) ["b"]=> int(22) ["c"]=> int(31) ["d"]=> int(47) }
Output for 5.4.13
array(4) { ["a"]=> int(0) ["b"]=> int(23) ["c"]=> int(28) ["d"]=> int(49) }
Output for 5.4.12
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(39) ["d"]=> int(42) }
Output for 5.3.22 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/5nsm3 on line 26
Process exited with code 255.