3v4l.org

run code in 300+ PHP versions simultaneously
<?php $valueWeights = [ 149 => 55.555, 130 => 10.0050, 131 => 5, 132 => 5.2, 133 => 10, 134 => 10.24, 135 => 5 ]; $mostDecimals = 0; // not bothering to validate against infinite and extremely fringe case floats foreach ($valueWeights as $value => $weight) { $tempDecimals = 0; while ((string)$weight !== (string)floor($weight)) { $weight *= 10; // this is not permanently mutating the weight ++$tempDecimals; } $mostDecimals = max($mostDecimals, $tempDecimals); } echo "Most Decimals: {$mostDecimals}\n"; $factor = pow(10, $mostDecimals); echo "Factor: " , $factor , "\n"; $totalWeight = (array_sum($valueWeights) - 1) * $factor; for ($i = 0; $i < 10; ++$i) { $rand = mt_rand(0, $totalWeight); echo "\nRand: " , $rand , "\n"; $cumulativeScaledWeight = 0; foreach ($valueWeights as $value => $weight) { $cumulativeScaledWeight += $weight * $factor; if ($rand < $cumulativeScaledWeight) { echo "Value: {$value}\n"; break; } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 7
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 7
Branch analysis from position: 16
Branch analysis from position: 7
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 43
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
2 jumps found. (Code = 77) Position 1 = 53, Position 2 = 65
Branch analysis from position: 53
2 jumps found. (Code = 78) Position 1 = 54, Position 2 = 65
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 64
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 43
Branch analysis from position: 69
Branch analysis from position: 43
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
Branch analysis from position: 65
Branch analysis from position: 65
Branch analysis from position: 22
filename:       /in/UdfW1
function name:  (null)
number of ops:  70
compiled vars:  !0 = $valueWeights, !1 = $mostDecimals, !2 = $weight, !3 = $value, !4 = $tempDecimals, !5 = $factor, !6 = $totalWeight, !7 = $i, !8 = $rand, !9 = $cumulativeScaledWeight
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   13     1        ASSIGN                                                   !1, 0
   15     2      > FE_RESET_R                                       $12     !0, ->22
          3    > > FE_FETCH_R                                       ~13     $12, !2, ->22
          4    >   ASSIGN                                                   !3, ~13
   16     5        ASSIGN                                                   !4, 0
   17     6      > JMP                                                      ->9
   18     7    >   ASSIGN_OP                                     3          !2, 10
   19     8        PRE_INC                                                  !4
   17     9    >   CAST                                          6  ~18     !2
         10        INIT_FCALL                                               'floor'
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $19     
         13        CAST                                          6  ~20     $19
         14        IS_NOT_IDENTICAL                                         ~18, ~20
         15      > JMPNZ                                                    ~21, ->7
   21    16    >   INIT_FCALL                                               'max'
         17        SEND_VAR                                                 !1
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                         $22     
         20        ASSIGN                                                   !1, $22
   15    21      > JMP                                                      ->3
         22    >   FE_FREE                                                  $12
   23    23        ROPE_INIT                                     3  ~25     'Most+Decimals%3A+'
         24        ROPE_ADD                                      1  ~25     ~25, !1
         25        ROPE_END                                      2  ~24     ~25, '%0A'
         26        ECHO                                                     ~24
   24    27        INIT_FCALL                                               'pow'
         28        SEND_VAL                                                 10
         29        SEND_VAR                                                 !1
         30        DO_ICALL                                         $27     
         31        ASSIGN                                                   !5, $27
   25    32        ECHO                                                     'Factor%3A+'
         33        ECHO                                                     !5
         34        ECHO                                                     '%0A'
   26    35        INIT_FCALL                                               'array_sum'
         36        SEND_VAR                                                 !0
         37        DO_ICALL                                         $29     
         38        SUB                                              ~30     $29, 1
         39        MUL                                              ~31     !5, ~30
         40        ASSIGN                                                   !6, ~31
   29    41        ASSIGN                                                   !7, 0
         42      > JMP                                                      ->67
   30    43    >   INIT_FCALL                                               'mt_rand'
         44        SEND_VAL                                                 0
         45        SEND_VAR                                                 !6
         46        DO_ICALL                                         $34     
         47        ASSIGN                                                   !8, $34
   31    48        ECHO                                                     '%0ARand%3A+'
         49        ECHO                                                     !8
         50        ECHO                                                     '%0A'
   32    51        ASSIGN                                                   !9, 0
   33    52      > FE_RESET_R                                       $37     !0, ->65
         53    > > FE_FETCH_R                                       ~38     $37, !2, ->65
         54    >   ASSIGN                                                   !3, ~38
   34    55        MUL                                              ~40     !2, !5
         56        ASSIGN_OP                                     1          !9, ~40
   35    57        IS_SMALLER                                               !8, !9
         58      > JMPZ                                                     ~42, ->64
   36    59    >   ROPE_INIT                                     3  ~44     'Value%3A+'
         60        ROPE_ADD                                      1  ~44     ~44, !3
         61        ROPE_END                                      2  ~43     ~44, '%0A'
         62        ECHO                                                     ~43
   37    63      > JMP                                                      ->65
   33    64    > > JMP                                                      ->53
         65    >   FE_FREE                                                  $37
   29    66        PRE_INC                                                  !7
         67    >   IS_SMALLER                                               !7, 10
         68      > JMPNZ                                                    ~47, ->43
   40    69    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.7 ms | 1014 KiB | 18 Q