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(8); $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 = substr($hash, 0, self::STATE_SIZE); return substr($hash, self::STATE_SIZE); } } $prng = new PseudoRandomSequence("test"); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); $prng = new PseudoRandomSequence("other"); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/945l6
function name:  (null)
number of ops:  65
compiled vars:  !0 = $prng
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $1      'PseudoRandomSequence'
          1        SEND_VAL_EX                                              'test'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   48     4        INIT_FCALL                                               'var_dump'
          5        INIT_METHOD_CALL                                         !0, 'int'
          6        SEND_VAL_EX                                              0
          7        SEND_VAL_EX                                              10
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR                                                 $4
         10        DO_ICALL                                                 
   49    11        INIT_FCALL                                               'var_dump'
         12        INIT_METHOD_CALL                                         !0, 'int'
         13        SEND_VAL_EX                                              0
         14        SEND_VAL_EX                                              10
         15        DO_FCALL                                      0  $6      
         16        SEND_VAR                                                 $6
         17        DO_ICALL                                                 
   50    18        INIT_FCALL                                               'var_dump'
         19        INIT_METHOD_CALL                                         !0, 'int'
         20        SEND_VAL_EX                                              0
         21        SEND_VAL_EX                                              10
         22        DO_FCALL                                      0  $8      
         23        SEND_VAR                                                 $8
         24        DO_ICALL                                                 
   51    25        INIT_FCALL                                               'var_dump'
         26        INIT_METHOD_CALL                                         !0, 'int'
         27        SEND_VAL_EX                                              0
         28        SEND_VAL_EX                                              10
         29        DO_FCALL                                      0  $10     
         30        SEND_VAR                                                 $10
         31        DO_ICALL                                                 
   52    32        NEW                                              $12     'PseudoRandomSequence'
         33        SEND_VAL_EX                                              'other'
         34        DO_FCALL                                      0          
         35        ASSIGN                                                   !0, $12
   53    36        INIT_FCALL                                               'var_dump'
         37        INIT_METHOD_CALL                                         !0, 'int'
         38        SEND_VAL_EX                                              0
         39        SEND_VAL_EX                                              100
         40        DO_FCALL                                      0  $15     
         41        SEND_VAR                                                 $15
         42        DO_ICALL                                                 
   54    43        INIT_FCALL                                               'var_dump'
         44        INIT_METHOD_CALL                                         !0, 'int'
         45        SEND_VAL_EX                                              0
         46        SEND_VAL_EX                                              100
         47        DO_FCALL                                      0  $17     
         48        SEND_VAR                                                 $17
         49        DO_ICALL                                                 
   55    50        INIT_FCALL                                               'var_dump'
         51        INIT_METHOD_CALL                                         !0, 'int'
         52        SEND_VAL_EX                                              0
         53        SEND_VAL_EX                                              100
         54        DO_FCALL                                      0  $19     
         55        SEND_VAR                                                 $19
         56        DO_ICALL                                                 
   56    57        INIT_FCALL                                               'var_dump'
         58        INIT_METHOD_CALL                                         !0, 'int'
         59        SEND_VAL_EX                                              0
         60        SEND_VAL_EX                                              100
         61        DO_FCALL                                      0  $21     
         62        SEND_VAR                                                 $21
         63        DO_ICALL                                                 
         64      > RETURN                                                   1

Class PseudoRandomSequence:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/945l6
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/945l6
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_VAL_EX                                              8
         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/945l6
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/945l6
function name:  mixState
number of ops:  20
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        INIT_FCALL                                               'substr'
          8        SEND_VAR                                                 !0
          9        SEND_VAL                                                 0
         10        SEND_VAL                                                 32
         11        DO_ICALL                                         $5      
         12        ASSIGN_OBJ                                               'state'
         13        OP_DATA                                                  $5
   43    14        INIT_FCALL                                               'substr'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 32
         17        DO_ICALL                                         $6      
         18      > RETURN                                                   $6
   44    19*     > RETURN                                                   null

End of function mixstate

End of class PseudoRandomSequence.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.08 ms | 1408 KiB | 29 Q