3v4l.org

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

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

End of function weight

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.76 ms | 1403 KiB | 22 Q