3v4l.org

run code in 500+ 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 = 19
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 19
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: 19
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
2 jumps found. (Code = 44) Position 1 = 66, Position 2 = 40
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 62
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 62
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 61
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 66, Position 2 = 40
Branch analysis from position: 66
Branch analysis from position: 40
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 62
Branch analysis from position: 62
Branch analysis from position: 19
filename:       /in/UdfW1
function name:  (null)
number of ops:  67
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, ->19
          3    > > FE_FETCH_R                                           ~13     $12, !2, ->19
          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    >   FRAMELESS_ICALL_2                max                 ~22     !1, !4
         17        ASSIGN                                                       !1, ~22
   15    18      > JMP                                                          ->3
         19    >   FE_FREE                                                      $12
   23    20        ROPE_INIT                                         3  ~25     'Most+Decimals%3A+'
         21        ROPE_ADD                                          1  ~25     ~25, !1
         22        ROPE_END                                          2  ~24     ~25, '%0A'
         23        ECHO                                                         ~24
   24    24        INIT_FCALL                                                   'pow'
         25        SEND_VAL                                                     10
         26        SEND_VAR                                                     !1
         27        DO_ICALL                                             $27     
         28        ASSIGN                                                       !5, $27
   25    29        ECHO                                                         'Factor%3A+'
         30        ECHO                                                         !5
         31        ECHO                                                         '%0A'
   26    32        INIT_FCALL                                                   'array_sum'
         33        SEND_VAR                                                     !0
         34        DO_ICALL                                             $29     
         35        SUB                                                  ~30     $29, 1
         36        MUL                                                  ~31     !5, ~30
         37        ASSIGN                                                       !6, ~31
   29    38        ASSIGN                                                       !7, 0
         39      > JMP                                                          ->64
   30    40    >   INIT_FCALL                                                   'mt_rand'
         41        SEND_VAL                                                     0
         42        SEND_VAR                                                     !6
         43        DO_ICALL                                             $34     
         44        ASSIGN                                                       !8, $34
   31    45        ECHO                                                         '%0ARand%3A+'
         46        ECHO                                                         !8
         47        ECHO                                                         '%0A'
   32    48        ASSIGN                                                       !9, 0
   33    49      > FE_RESET_R                                           $37     !0, ->62
         50    > > FE_FETCH_R                                           ~38     $37, !2, ->62
         51    >   ASSIGN                                                       !3, ~38
   34    52        MUL                                                  ~40     !2, !5
         53        ASSIGN_OP                                         1          !9, ~40
   35    54        IS_SMALLER                                                   !8, !9
         55      > JMPZ                                                         ~42, ->61
   36    56    >   ROPE_INIT                                         3  ~44     'Value%3A+'
         57        ROPE_ADD                                          1  ~44     ~44, !3
         58        ROPE_END                                          2  ~43     ~44, '%0A'
         59        ECHO                                                         ~43
   37    60      > JMP                                                          ->62
   33    61    > > JMP                                                          ->50
         62    >   FE_FREE                                                      $37
   29    63        PRE_INC                                                      !7
         64    >   IS_SMALLER                                                   !7, 10
         65      > JMPNZ                                                        ~47, ->40
   40    66    > > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.93 ms | 1323 KiB | 17 Q