3v4l.org

run code in 300+ PHP versions simultaneously
<?php function weight($array) { $iter = 0; $sum = array_sum($array); $rnd = mt_rand(0,$sum); foreach($array as $k=>$v) { $iter++; if($rnd <= $v) { var_dump($iter); return $k; } $rnd = $rnd - $v; } } $weights = [100, 200, 1000]; $values = array_fill(0, count($weights), 0); for ($x = 0; $x < 1000; $x++) { $w = weight($weights); $values[$w] = $values[$w] + 1; } var_dump($values);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 10
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 10
Branch analysis from position: 21
Branch analysis from position: 10
filename:       /in/sZBWV
function name:  (null)
number of ops:  25
compiled vars:  !0 = $weights, !1 = $values, !2 = $x, !3 = $w
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   ASSIGN                                                   !0, <array>
   21     1        INIT_FCALL                                               'array_fill'
          2        SEND_VAL                                                 0
          3        COUNT                                            ~5      !0
          4        SEND_VAL                                                 ~5
          5        SEND_VAL                                                 0
          6        DO_ICALL                                         $6      
          7        ASSIGN                                                   !1, $6
   23     8        ASSIGN                                                   !2, 0
          9      > JMP                                                      ->19
   25    10    >   INIT_FCALL                                               'weight'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $9      
         13        ASSIGN                                                   !3, $9
   26    14        FETCH_DIM_R                                      ~12     !1, !3
         15        ADD                                              ~13     ~12, 1
         16        ASSIGN_DIM                                               !1, !3
         17        OP_DATA                                                  ~13
   23    18        PRE_INC                                                  !2
         19    >   IS_SMALLER                                               !2, 1000
         20      > JMPNZ                                                    ~15, ->10
   29    21    >   INIT_FCALL                                               'var_dump'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                                 
         24      > RETURN                                                   1

Function weight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 25
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 25
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/sZBWV
function name:  weight
number of ops:  27
compiled vars:  !0 = $array, !1 = $iter, !2 = $sum, !3 = $rnd, !4 = $v, !5 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, 0
    5     2        INIT_FCALL                                               'array_sum'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $7      
          5        ASSIGN                                                   !2, $7
    6     6        INIT_FCALL                                               'mt_rand'
          7        SEND_VAL                                                 0
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $9      
         10        ASSIGN                                                   !3, $9
    7    11      > FE_RESET_R                                       $11     !0, ->25
         12    > > FE_FETCH_R                                       ~12     $11, !4, ->25
         13    >   ASSIGN                                                   !5, ~12
    9    14        PRE_INC                                                  !1
   10    15        IS_SMALLER_OR_EQUAL                                      !3, !4
         16      > JMPZ                                                     ~15, ->22
   12    17    >   INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                                 
   13    20        FE_FREE                                                  $11
         21      > RETURN                                                   !5
   15    22    >   SUB                                              ~17     !3, !4
         23        ASSIGN                                                   !3, ~17
    7    24      > JMP                                                      ->12
         25    >   FE_FREE                                                  $11
   17    26      > RETURN                                                   null

End of function weight

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.28 ms | 1407 KiB | 22 Q