3v4l.org

run code in 300+ PHP versions simultaneously
<?php function weight($array) { $sum = array_sum($array); $rnd = mt_rand(0,$sum); foreach($array as $k=>$v) { if($rnd <= $v) { 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/DFPFJ
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
-------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                   !0, <array>
   19     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
   21     8        ASSIGN                                                   !2, 0
          9      > JMP                                                      ->19
   23    10    >   INIT_FCALL                                               'weight'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $9      
         13        ASSIGN                                                   !3, $9
   24    14        FETCH_DIM_R                                      ~12     !1, !3
         15        ADD                                              ~13     ~12, 1
         16        ASSIGN_DIM                                               !1, !3
         17        OP_DATA                                                  ~13
   21    18        PRE_INC                                                  !2
         19    >   IS_SMALLER                                               !2, 1000
         20      > JMPNZ                                                    ~15, ->10
   27    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 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/DFPFJ
function name:  weight
number of ops:  22
compiled vars:  !0 = $array, !1 = $sum, !2 = $rnd, !3 = $v, !4 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_sum'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
    6     5        INIT_FCALL                                               'mt_rand'
          6        SEND_VAL                                                 0
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $7      
          9        ASSIGN                                                   !2, $7
    7    10      > FE_RESET_R                                       $9      !0, ->20
         11    > > FE_FETCH_R                                       ~10     $9, !3, ->20
         12    >   ASSIGN                                                   !4, ~10
    9    13        IS_SMALLER_OR_EQUAL                                      !2, !3
         14      > JMPZ                                                     ~12, ->17
   11    15    >   FE_FREE                                                  $9
         16      > RETURN                                                   !4
   13    17    >   SUB                                              ~13     !2, !3
         18        ASSIGN                                                   !2, ~13
    7    19      > JMP                                                      ->11
         20    >   FE_FREE                                                  $9
   15    21      > RETURN                                                   null

End of function weight

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.14 ms | 1403 KiB | 22 Q