3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 7.0.2, 7.4.0
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(26) ["d"]=> int(56) }
Output for 5.5.34, 7.3.12
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(37) ["d"]=> int(50) }
Output for 5.5.31 - 5.5.32, 7.3.11
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(34) ["d"]=> int(51) }
Output for 7.3.10
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(40) ["d"]=> int(48) }
Output for 7.3.9
array(4) { ["a"]=> int(0) ["b"]=> int(24) ["c"]=> int(25) ["d"]=> int(51) }
Output for 7.3.8
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(25) ["d"]=> int(63) }
Output for 7.3.7
array(4) { ["a"]=> int(0) ["b"]=> int(10) ["c"]=> int(32) ["d"]=> int(58) }
Output for 7.0.1, 7.3.6
array(4) { ["a"]=> int(0) ["b"]=> int(25) ["c"]=> int(35) ["d"]=> int(40) }
Output for 7.2.24, 7.3.5
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(34) ["d"]=> int(47) }
Output for 7.3.4
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(33) ["d"]=> int(49) }
Output for 7.3.3
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(35) ["d"]=> int(45) }
Output for 7.1.33, 7.3.2
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(36) ["d"]=> int(46) }
Output for 7.3.1
array(4) { ["a"]=> int(0) ["b"]=> int(16) ["c"]=> int(29) ["d"]=> int(55) }
Output for 5.5.25, 7.3.0
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(41) ["d"]=> int(44) }
Output for 7.2.25
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(37) ["d"]=> int(49) }
Output for 7.2.23
array(4) { ["a"]=> int(0) ["b"]=> int(17) ["c"]=> int(38) ["d"]=> int(45) }
Output for 7.2.22
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(34) ["d"]=> int(53) }
Output for 7.2.21
array(4) { ["a"]=> int(0) ["b"]=> int(11) ["c"]=> int(30) ["d"]=> int(59) }
Output for 7.2.20
array(4) { ["a"]=> int(0) ["b"]=> int(28) ["c"]=> int(32) ["d"]=> int(40) }
Output for 7.2.19
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(38) ["d"]=> int(43) }
Output for 7.2.18
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(36) ["d"]=> int(44) }
Output for 7.2.17
array(4) { ["a"]=> int(0) ["b"]=> int(18) ["c"]=> int(37) ["d"]=> int(45) }
Output for 7.2.0
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(42) ["d"]=> int(45) }
Output for 7.1.32
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(27) ["d"]=> int(58) }
Output for 7.1.31
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(42) ["d"]=> int(46) }
Output for 7.1.30
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(30) ["d"]=> int(55) }
Output for 5.6.15, 7.1.29
array(4) { ["a"]=> int(0) ["b"]=> int(19) ["c"]=> int(35) ["d"]=> int(46) }
Output for 5.5.29, 7.1.28
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(36) ["d"]=> int(49) }
Output for 5.6.10, 7.1.27
array(4) { ["a"]=> int(0) ["b"]=> int(14) ["c"]=> int(35) ["d"]=> int(51) }
Output for 7.1.26
array(4) { ["a"]=> int(0) ["b"]=> int(15) ["c"]=> int(23) ["d"]=> int(62) }
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.0
array(4) { ["a"]=> int(0) ["b"]=> int(13) ["c"]=> int(33) ["d"]=> int(54) }
Output for 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.14
array(4) { ["a"]=> int(0) ["b"]=> int(20) ["c"]=> int(30) ["d"]=> int(50) }
Output for 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.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.30
array(4) { ["a"]=> int(0) ["b"]=> int(8) ["c"]=> int(33) ["d"]=> int(59) }
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.24
array(4) { ["a"]=> int(0) ["b"]=> int(12) ["c"]=> int(32) ["d"]=> int(56) }

preferences:
99.31 ms | 402 KiB | 76 Q