3v4l.org

run code in 300+ PHP versions simultaneously
<?php class PseudoRandomSequence { const STATE_HASH = "sha512"; // 1/2 STATE_HASH block size, const STATE_SIZE = 32; private $state; public function __construct($state) { $this->state = $state; $this->mixState(); } public function int($min = 0, $max = PHP_INT_MAX) { $range = $max - $min; if ($range <= 0 || is_float($range)) { throw new Exception("Invalid min/max provided"); } $bits = 0; $tmp = $range; while ($tmp >>= 1) { $bits++; } $bytes = (int) max(ceil($bits / 8), 1); $mask = pow(2, $bits) - 1; do { $rand = $this->bytes($bytes); $result = hexdec(bin2hex($rand)) & $mask; } while ($result > $range); return $result + $min; } public function bytes($n) { $ret = ''; do { $ret .= $this->mixState(); } while (strlen($ret) < $n); return substr($ret, 0, $n); } private function mixState() { $hash = hash(self::STATE_HASH, $this->state, true); $this->state = $hash; return substr($hash, self::STATE_SIZE); } } $prng = new PseudoRandomSequence("blah"); for ($i = 0; $i < 100; $i++) { var_dump($prng->int()); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
Branch analysis from position: 6
filename:       /in/rgcTO
function name:  (null)
number of ops:  15
compiled vars:  !0 = $prng, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $2      'PseudoRandomSequence'
          1        SEND_VAL_EX                                              'blah'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   48     4        ASSIGN                                                   !1, 0
          5      > JMP                                                      ->12
   49     6    >   INIT_FCALL                                               'var_dump'
          7        INIT_METHOD_CALL                                         !0, 'int'
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
   48    11        PRE_INC                                                  !1
         12    >   IS_SMALLER                                               !1, 100
         13      > JMPNZ                                                    ~9, ->6
   50    14    > > RETURN                                                   1

Class PseudoRandomSequence:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rgcTO
function name:  __construct
number of ops:  6
compiled vars:  !0 = $state
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1        ASSIGN_OBJ                                               'state'
          2        OP_DATA                                                  !0
   10     3        INIT_METHOD_CALL                                         'mixState'
          4        DO_FCALL                                      0          
   11     5      > RETURN                                                   null

End of function __construct

Function int:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 16
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 35
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 16
Branch analysis from position: 19
Branch analysis from position: 16
Branch analysis from position: 8
filename:       /in/rgcTO
function name:  int
number of ops:  52
compiled vars:  !0 = $min, !1 = $max, !2 = $range, !3 = $bits, !4 = $tmp, !5 = $bytes, !6 = $mask, !7 = $rand, !8 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV_INIT                                        !0      0
          1        RECV_INIT                                        !1      <const ast>
   14     2        SUB                                              ~9      !1, !0
          3        ASSIGN                                                   !2, ~9
   15     4        IS_SMALLER_OR_EQUAL                              ~11     !2, 0
          5      > JMPNZ_EX                                         ~11     ~11, ->8
          6    >   TYPE_CHECK                                   32  ~12     !2
          7        BOOL                                             ~11     ~12
          8    > > JMPZ                                                     ~11, ->13
   16     9    >   NEW                                              $13     'Exception'
         10        SEND_VAL_EX                                              'Invalid+min%2Fmax+provided'
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $13
   18    13    >   ASSIGN                                                   !3, 0
   19    14        ASSIGN                                                   !4, !2
   20    15      > JMP                                                      ->17
   21    16    >   PRE_INC                                                  !3
   20    17    >   ASSIGN_OP                                     7  ~18     !4, 1
         18      > JMPNZ                                                    ~18, ->16
   23    19    >   INIT_FCALL                                               'max'
         20        INIT_FCALL                                               'ceil'
         21        DIV                                              ~19     !3, 8
         22        SEND_VAL                                                 ~19
         23        DO_ICALL                                         $20     
         24        SEND_VAR                                                 $20
         25        SEND_VAL                                                 1
         26        DO_ICALL                                         $21     
         27        CAST                                          4  ~22     $21
         28        ASSIGN                                                   !5, ~22
   24    29        INIT_FCALL                                               'pow'
         30        SEND_VAL                                                 2
         31        SEND_VAR                                                 !3
         32        DO_ICALL                                         $24     
         33        SUB                                              ~25     $24, 1
         34        ASSIGN                                                   !6, ~25
   26    35    >   INIT_METHOD_CALL                                         'bytes'
         36        SEND_VAR_EX                                              !5
         37        DO_FCALL                                      0  $27     
         38        ASSIGN                                                   !7, $27
   27    39        INIT_FCALL                                               'hexdec'
         40        INIT_FCALL                                               'bin2hex'
         41        SEND_VAR                                                 !7
         42        DO_ICALL                                         $29     
         43        SEND_VAR                                                 $29
         44        DO_ICALL                                         $30     
         45        BW_AND                                           ~31     !6, $30
         46        ASSIGN                                                   !8, ~31
   28    47        IS_SMALLER                                               !2, !8
         48      > JMPNZ                                                    ~33, ->35
   29    49    >   ADD                                              ~34     !8, !0
         50      > RETURN                                                   ~34
   30    51*     > RETURN                                                   null

End of function int

Function bytes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
filename:       /in/rgcTO
function name:  bytes
number of ops:  15
compiled vars:  !0 = $n, !1 = $ret
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        ASSIGN                                                   !1, ''
   35     2    >   INIT_METHOD_CALL                                         'mixState'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN_OP                                     8          !1, $3
   36     5        STRLEN                                           ~5      !1
          6        IS_SMALLER                                               ~5, !0
          7      > JMPNZ                                                    ~6, ->2
   37     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !1
         10        SEND_VAL                                                 0
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $7      
         13      > RETURN                                                   $7
   38    14*     > RETURN                                                   null

End of function bytes

Function mixstate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rgcTO
function name:  mixState
number of ops:  15
compiled vars:  !0 = $hash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   INIT_FCALL                                               'hash'
          1        SEND_VAL                                                 'sha512'
          2        FETCH_OBJ_R                                      ~1      'state'
          3        SEND_VAL                                                 ~1
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $2      
          6        ASSIGN                                                   !0, $2
   42     7        ASSIGN_OBJ                                               'state'
          8        OP_DATA                                                  !0
   43     9        INIT_FCALL                                               'substr'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 32
         12        DO_ICALL                                         $5      
         13      > RETURN                                                   $5
   44    14*     > RETURN                                                   null

End of function mixstate

End of class PseudoRandomSequence.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.27 ms | 1404 KiB | 29 Q