3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Random_* Compatibility Library * for using the new PHP 7 random_* API in PHP 5 projects * * The MIT License (MIT) * * Copyright (c) 2015 Paragon Initiative Enterprises * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ if (!function_exists('random_int')) { /** * Fetch a random integer between $min and $max inclusive * * @param int $min * @param int $max * * @throws Exception * * @return int */ function random_int($min, $max) { /** * Type and input logic checks */ if (!is_int($min)) { throw new Exception( 'random_int(): $min must be an integer' ); } if (!is_int($max)) { throw new Exception( 'random_int(): $max must be an integer' ); } if ($min > $max) { throw new Exception( 'Minimum value must be less than or equal to the maximum value' ); } if ($max === $min) { return $min; } /** * Initialize variables to 0 * * We want to store: * $bytes => the number of random bytes we need * $mask => an integer bitmask (for use with the &) operator * so we can minimize the number of discards */ $attempts = $bits = $bytes = $mask = $valueShift = 0; /** * At this point, $range is a positive number greater than 0. It might * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to * a float and we will lose some precision. */ $range = $max - $min; /** * Test for integer overflow: */ if (!is_int($range)) { /** * Still safely calculate wider ranges. * Provided by @CodesInChaos, @oittaa * * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 * * We use ~0 as a mask in this case because it generates all 1s * * @ref https://eval.in/400356 (32-bit) * @ref http://3v4l.org/XX9r5 (64-bit) */ $bytes = PHP_INT_SIZE; $mask = ~0; } else { /** * $bits is effectively ceil(log($range, 2)) without dealing with * type juggling */ while ($range > 0) { if ($bits % 8 === 0) { ++$bytes; } ++$bits; $range >>= 1; $mask = $mask << 1 | 1; } $valueShift = $min; } /** * Now that we have our parameters set up, let's begin generating * random integers until one falls between $min and $max */ do { /** * The rejection probability is at most 0.5, so this corresponds * to a failure probability of 2^-128 for a working RNG */ if ($attempts > 128) { throw new Exception( 'random_int: RNG is broken - too many rejections' ); } /** * Let's grab the necessary number of random bytes */ $randomByteString = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); if ($randomByteString === false) { throw new Exception( 'Random number generator failure' ); } /** * Let's turn $randomByteString into an integer * * This uses bitwise operators (<< and |) to build an integer * out of the values extracted from ord() * * Example: [9F] | [6D] | [32] | [0C] => * 159 + 27904 + 3276800 + 201326592 => * 204631455 */ $val = 0; for ($i = 0; $i < $bytes; ++$i) { $val |= ord($randomByteString[$i]) << ($i * 8); } /** * Apply mask */ $val &= $mask; $val += $valueShift; ++$attempts; /** * If $val overflows to a floating point number, * ... or is larger than $max, * ... or smaller than $int, * then try again. */ } while (!is_int($val) || $val > $max || $val < $min); return (int) $val; } } function random_unique_select($num, array $possible_values) { $sizeof = count($possible_values); if ($num > $sizeof) { throw new InvalidArgumentException('$num is too large'); } $selected = []; for ($i = 0; $i < $num; ++$i) { $r = random_int(0, $sizeof - 1); $selected[] = $possible_values[$r]; unset($possible_values[$r]); --$sizeof; $possible_values = array_values($possible_values); } return $selected; } $lottery = random_unique_select(6, range(1,29)); var_dump($lottery);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/Hb97A
function name:  (null)
number of ops:  19
compiled vars:  !0 = $lottery
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'random_int'
          2        DO_ICALL                                         $1      
          3        BOOL_NOT                                         ~2      $1
          4      > JMPZ                                                     ~2, ->6
   42     5    >   DECLARE_FUNCTION                                         'random_int'
  192     6    >   INIT_FCALL                                               'random_unique_select'
          7        SEND_VAL                                                 6
          8        INIT_FCALL                                               'range'
          9        SEND_VAL                                                 1
         10        SEND_VAL                                                 29
         11        DO_ICALL                                         $3      
         12        SEND_VAR                                                 $3
         13        DO_FCALL                                      0  $4      
         14        ASSIGN                                                   !0, $4
  194    15        INIT_FCALL                                               'var_dump'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                                 
         18      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 22
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 57
Branch analysis from position: 53
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 69
Branch analysis from position: 65
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 72
Branch analysis from position: 82
2 jumps found. (Code = 47) Position 1 = 88, Position 2 = 90
Branch analysis from position: 88
2 jumps found. (Code = 47) Position 1 = 91, Position 2 = 93
Branch analysis from position: 91
2 jumps found. (Code = 44) Position 1 = 94, Position 2 = 51
Branch analysis from position: 94
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 93
Branch analysis from position: 90
Branch analysis from position: 72
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 72
Branch analysis from position: 82
Branch analysis from position: 72
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 39
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 57
Branch analysis from position: 53
Branch analysis from position: 57
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 43
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 39
Branch analysis from position: 50
Branch analysis from position: 39
Branch analysis from position: 43
filename:       /in/Hb97A
function name:  random_int
number of ops:  97
compiled vars:  !0 = $min, !1 = $max, !2 = $attempts, !3 = $bits, !4 = $bytes, !5 = $mask, !6 = $valueShift, !7 = $range, !8 = $randomByteString, !9 = $val, !10 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        TYPE_CHECK                                   16  ~11     !0
          3        BOOL_NOT                                         ~12     ~11
          4      > JMPZ                                                     ~12, ->9
   48     5    >   NEW                                              $13     'Exception'
   49     6        SEND_VAL_EX                                              'random_int%28%29%3A+%24min+must+be+an+integer'
   48     7        DO_FCALL                                      0          
   49     8      > THROW                                         0          $13
   52     9    >   TYPE_CHECK                                   16  ~15     !1
         10        BOOL_NOT                                         ~16     ~15
         11      > JMPZ                                                     ~16, ->16
   53    12    >   NEW                                              $17     'Exception'
   54    13        SEND_VAL_EX                                              'random_int%28%29%3A+%24max+must+be+an+integer'
   53    14        DO_FCALL                                      0          
   54    15      > THROW                                         0          $17
   57    16    >   IS_SMALLER                                               !1, !0
         17      > JMPZ                                                     ~19, ->22
   58    18    >   NEW                                              $20     'Exception'
   59    19        SEND_VAL_EX                                              'Minimum+value+must+be+less+than+or+equal+to+the+maximum+value'
   58    20        DO_FCALL                                      0          
   59    21      > THROW                                         0          $20
   62    22    >   IS_IDENTICAL                                             !1, !0
         23      > JMPZ                                                     ~22, ->25
   63    24    > > RETURN                                                   !0
   74    25    >   ASSIGN                                           ~23     !6, 0
         26        ASSIGN                                           ~24     !5, ~23
         27        ASSIGN                                           ~25     !4, ~24
         28        ASSIGN                                           ~26     !3, ~25
         29        ASSIGN                                                   !2, ~26
   81    30        SUB                                              ~28     !1, !0
         31        ASSIGN                                                   !7, ~28
   86    32        TYPE_CHECK                                   16  ~30     !7
         33        BOOL_NOT                                         ~31     ~30
         34      > JMPZ                                                     ~31, ->38
   98    35    >   ASSIGN                                                   !4, 8
   99    36        ASSIGN                                                   !5, -1
   86    37      > JMP                                                      ->51
  105    38    > > JMP                                                      ->48
  106    39    >   MOD                                              ~34     !3, 8
         40        IS_IDENTICAL                                             ~34, 0
         41      > JMPZ                                                     ~35, ->43
  107    42    >   PRE_INC                                                  !4
  109    43    >   PRE_INC                                                  !3
  110    44        ASSIGN_OP                                     7          !7, 1
  111    45        SL                                               ~39     !5, 1
         46        BW_OR                                            ~40     ~39, 1
         47        ASSIGN                                                   !5, ~40
  105    48    >   IS_SMALLER                                               0, !7
         49      > JMPNZ                                                    ~42, ->39
  113    50    >   ASSIGN                                                   !6, !0
  125    51    >   IS_SMALLER                                               128, !2
         52      > JMPZ                                                     ~44, ->57
  126    53    >   NEW                                              $45     'Exception'
  127    54        SEND_VAL_EX                                              'random_int%3A+RNG+is+broken+-+too+many+rejections'
  126    55        DO_FCALL                                      0          
  127    56      > THROW                                         0          $45
  134    57    >   INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
         58        SEND_VAR_EX                                              !4
         59        FETCH_CONSTANT                                   ~47     'MCRYPT_DEV_URANDOM'
         60        SEND_VAL_EX                                              ~47
         61        DO_FCALL                                      0  $48     
         62        ASSIGN                                                   !8, $48
  135    63        TYPE_CHECK                                    4          !8
         64      > JMPZ                                                     ~50, ->69
  136    65    >   NEW                                              $51     'Exception'
  137    66        SEND_VAL_EX                                              'Random+number+generator+failure'
  136    67        DO_FCALL                                      0          
  137    68      > THROW                                         0          $51
  151    69    >   ASSIGN                                                   !9, 0
  152    70        ASSIGN                                                   !10, 0
         71      > JMP                                                      ->80
  153    72    >   INIT_FCALL                                               'ord'
         73        FETCH_DIM_R                                      ~55     !8, !10
         74        SEND_VAL                                                 ~55
         75        DO_ICALL                                         $56     
         76        MUL                                              ~57     !10, 8
         77        SL                                               ~58     $56, ~57
         78        ASSIGN_OP                                     9          !9, ~58
  152    79        PRE_INC                                                  !10
         80    >   IS_SMALLER                                               !10, !4
         81      > JMPNZ                                                    ~61, ->72
  159    82    >   ASSIGN_OP                                    10          !9, !5
  160    83        ASSIGN_OP                                     1          !9, !6
  162    84        PRE_INC                                                  !2
  169    85        TYPE_CHECK                                   16  ~65     !9
         86        BOOL_NOT                                         ~66     ~65
         87      > JMPNZ_EX                                         ~66     ~66, ->90
         88    >   IS_SMALLER                                       ~67     !1, !9
         89        BOOL                                             ~66     ~67
         90    > > JMPNZ_EX                                         ~66     ~66, ->93
         91    >   IS_SMALLER                                       ~68     !9, !0
         92        BOOL                                             ~66     ~68
         93    > > JMPNZ                                                    ~66, ->51
  170    94    >   CAST                                          4  ~69     !9
         95      > RETURN                                                   ~69
  171    96*     > RETURN                                                   null

End of Dynamic Function 0

Function random_unique_select:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 13
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 13
Branch analysis from position: 31
Branch analysis from position: 13
filename:       /in/Hb97A
function name:  random_unique_select
number of ops:  33
compiled vars:  !0 = $num, !1 = $possible_values, !2 = $sizeof, !3 = $selected, !4 = $i, !5 = $r
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  174     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  176     2        COUNT                                            ~6      !1
          3        ASSIGN                                                   !2, ~6
  177     4        IS_SMALLER                                               !2, !0
          5      > JMPZ                                                     ~8, ->10
  178     6    >   NEW                                              $9      'InvalidArgumentException'
          7        SEND_VAL_EX                                              '%24num+is+too+large'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $9
  180    10    >   ASSIGN                                                   !3, <array>
  181    11        ASSIGN                                                   !4, 0
         12      > JMP                                                      ->29
  182    13    >   INIT_FCALL                                               'random_int'
         14        SEND_VAL                                                 0
         15        SUB                                              ~13     !2, 1
         16        SEND_VAL                                                 ~13
         17        DO_ICALL                                         $14     
         18        ASSIGN                                                   !5, $14
  183    19        FETCH_DIM_R                                      ~17     !1, !5
         20        ASSIGN_DIM                                               !3
         21        OP_DATA                                                  ~17
  184    22        UNSET_DIM                                                !1, !5
  185    23        PRE_DEC                                                  !2
  186    24        INIT_FCALL                                               'array_values'
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $19     
         27        ASSIGN                                                   !1, $19
  181    28        PRE_INC                                                  !4
         29    >   IS_SMALLER                                               !4, !0
         30      > JMPNZ                                                    ~22, ->13
  188    31    > > RETURN                                                   !3
  189    32*     > RETURN                                                   null

End of function random_unique_select

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.23 ms | 1030 KiB | 20 Q