3v4l.org

run code in 500+ 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']; $weights = [0, 3, 6]; $count = array_map(function () { return 0; }, array_flip($items)); $i = 100; do { $x = random_weighted($items, $weights); ++$count[$x]; } while (--$i); var_dump($count, array_sum($count));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 12
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/3uiKq
function name:  (null)
number of ops:  29
compiled vars:  !0 = $items, !1 = $weights, !2 = $count, !3 = $i, !4 = $x
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                       !0, <array>
   27     1        ASSIGN                                                       !1, <array>
   28     2        INIT_FCALL                                                   'array_map'
          3        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
          4        SEND_VAL                                                     ~7
          5        INIT_FCALL                                                   'array_flip'
          6        SEND_VAR                                                     !0
          7        DO_ICALL                                             $8      
          8        SEND_VAR                                                     $8
          9        DO_ICALL                                             $9      
         10        ASSIGN                                                       !2, $9
   29    11        ASSIGN                                                       !3, 100
   32    12    >   INIT_FCALL                                                   'random_weighted'
         13        SEND_VAR                                                     !0
         14        SEND_VAR                                                     !1
         15        DO_FCALL                                          0  $12     
         16        ASSIGN                                                       !4, $12
   33    17        FETCH_DIM_RW                                         $14     !2, !4
         18        PRE_INC                                                      $14
   34    19        PRE_DEC                                              ~16     !3
         20      > JMPNZ                                                        ~16, ->12
   36    21    >   INIT_FCALL                                                   'var_dump'
         22        SEND_VAR                                                     !2
         23        INIT_FCALL                                                   'array_sum'
         24        SEND_VAR                                                     !2
         25        DO_ICALL                                             $17     
         26        SEND_VAR                                                     $17
         27        DO_ICALL                                                     
         28      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3uiKq
function name:  {closure:/in/3uiKq:28}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E > > RETURN                                                       0
          1*     > RETURN                                                       null

End of Dynamic Function 0

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/3uiKq
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.5.0


preferences:
131.33 ms | 1022 KiB | 16 Q