3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getRandom($values, $weights) { $count = count($values); $i = 0; $n = 0; $num = mt_rand(0, array_sum($weights) * 100) / 100; while($i < $count) { $n += $weights[$i]; if($n >= $num) break; $i++; } return $values[$i]; } $values = [1,2,3,4]; $weights = [60.0000,30.0000,9.9800,.0200]; $counts = array_combine($values, array_fill(0, count($values), 0)); for ($i = 0; $i < 100000; $i++) { $counts[getRandom($values, $weights)]++; } echo "value\tweight\tcount\t%\n"; for ($i = 0; $i < count($values); $i++) { echo $values[$i] . "\t" . $weights[$i] . "\t" . $counts[$values[$i]] . "\t" . round($counts[$values[$i]]/array_sum($counts)*100, 2) . "\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 15
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 27
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 27
Branch analysis from position: 54
Branch analysis from position: 27
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 15
Branch analysis from position: 24
Branch analysis from position: 15
filename:       /in/cvP9P
function name:  (null)
number of ops:  55
compiled vars:  !0 = $values, !1 = $weights, !2 = $counts, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   ASSIGN                                                   !0, <array>
   20     1        ASSIGN                                                   !1, <array>
   21     2        INIT_FCALL                                               'array_combine'
          3        SEND_VAR                                                 !0
          4        INIT_FCALL                                               'array_fill'
          5        SEND_VAL                                                 0
          6        COUNT                                            ~6      !0
          7        SEND_VAL                                                 ~6
          8        SEND_VAL                                                 0
          9        DO_ICALL                                         $7      
         10        SEND_VAR                                                 $7
         11        DO_ICALL                                         $8      
         12        ASSIGN                                                   !2, $8
   22    13        ASSIGN                                                   !3, 0
         14      > JMP                                                      ->22
   23    15    >   INIT_FCALL                                               'getrandom'
         16        SEND_VAR                                                 !0
         17        SEND_VAR                                                 !1
         18        DO_FCALL                                      0  $11     
         19        FETCH_DIM_RW                                     $12     !2, $11
         20        PRE_INC                                                  $12
   22    21        PRE_INC                                                  !3
         22    >   IS_SMALLER                                               !3, 100000
         23      > JMPNZ                                                    ~15, ->15
   26    24    >   ECHO                                                     'value%09weight%09count%09%25%0A'
   27    25        ASSIGN                                                   !3, 0
         26      > JMP                                                      ->51
   28    27    >   FETCH_DIM_R                                      ~17     !0, !3
         28        CONCAT                                           ~18     ~17, '%09'
         29        FETCH_DIM_R                                      ~19     !1, !3
         30        CONCAT                                           ~20     ~18, ~19
         31        CONCAT                                           ~21     ~20, '%09'
         32        FETCH_DIM_R                                      ~22     !0, !3
         33        FETCH_DIM_R                                      ~23     !2, ~22
         34        CONCAT                                           ~24     ~21, ~23
         35        CONCAT                                           ~25     ~24, '%09'
         36        INIT_FCALL                                               'round'
         37        FETCH_DIM_R                                      ~26     !0, !3
         38        FETCH_DIM_R                                      ~27     !2, ~26
         39        INIT_FCALL                                               'array_sum'
         40        SEND_VAR                                                 !2
         41        DO_ICALL                                         $28     
         42        DIV                                              ~29     ~27, $28
         43        MUL                                              ~30     ~29, 100
         44        SEND_VAL                                                 ~30
         45        SEND_VAL                                                 2
         46        DO_ICALL                                         $31     
         47        CONCAT                                           ~32     ~25, $31
         48        CONCAT                                           ~33     ~32, '%0A'
         49        ECHO                                                     ~33
   27    50        PRE_INC                                                  !3
         51    >   COUNT                                            ~35     !0
         52        IS_SMALLER                                               !3, ~35
         53      > JMPNZ                                                    ~36, ->27
   29    54    > > RETURN                                                   1

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

End of function getrandom

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
129.83 ms | 1026 KiB | 19 Q