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, 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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 4
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
filename:       /in/r5g49
function name:  (null)
number of ops:  17
compiled vars:  !0 = $items, !1 = $weights, !2 = $i, !3 = $results, !4 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   27     1        ASSIGN                                                   !1, <array>
   28     2        ASSIGN                                                   !2, 100
   29     3        ASSIGN                                                   !3, <array>
   32     4    >   INIT_FCALL                                               'random_weighted'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $9      
          8        ASSIGN                                                   !4, $9
   33     9        FETCH_DIM_RW                                     $11     !3, !4
         10        PRE_INC                                                  $11
   35    11        PRE_DEC                                          ~13     !2
         12      > JMPNZ                                                    ~13, ->4
   37    13    >   INIT_FCALL                                               'var_dump'
         14        SEND_VAR                                                 !3
         15        DO_ICALL                                                 
         16      > RETURN                                                   1

Function random_weighted:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 11
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 11
Branch analysis from position: 27
Branch analysis from position: 11
Branch analysis from position: 22
filename:       /in/r5g49
function name:  random_weighted
number of ops:  29
compiled vars:  !0 = $items, !1 = $weights, !2 = $weight, !3 = $return, !4 = $i, !5 = $n, !6 = $random
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN                                                   !2, 0
   11     3        INIT_FCALL                                               'current'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $8      
          6        ASSIGN                                                   !3, $8
   13     7        ASSIGN                                                   !4, 0
          8        COUNT                                            ~11     !0
          9        ASSIGN                                                   !5, ~11
         10      > JMP                                                      ->25
   14    11    >   INIT_FCALL                                               'mt_rand'
         12        SEND_VAL                                                 0
         13        FETCH_DIM_R                                      ~13     !1, !4
         14        ADD                                              ~14     ~13, !2
         15        SEND_VAL                                                 ~14
         16        DO_ICALL                                         $15     
         17        ASSIGN                                                   !6, $15
   16    18        IS_SMALLER_OR_EQUAL                                      !2, !6
         19      > JMPZ                                                     ~17, ->22
   17    20    >   FETCH_DIM_R                                      ~18     !0, !4
         21        ASSIGN                                                   !3, ~18
   20    22    >   FETCH_DIM_R                                      ~20     !1, !4
         23        ASSIGN_OP                                     1          !2, ~20
   13    24        PRE_INC                                                  !4
         25    >   IS_SMALLER                                               !4, !5
         26      > JMPNZ                                                    ~23, ->11
   23    27    > > RETURN                                                   !3
   24    28*     > RETURN                                                   null

End of function random_weighted

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.6 ms | 1402 KiB | 20 Q