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 = 1; $tmp = $range; while ($tmp >>= 1) { $bits++; } $bytes = (int) max(ceil($bits / 8), 1); $mask = (int) (pow(2, $bits) - 1); if ($mask < 0) { // Hack since PHP will upcast 2^63-1 into a negative number $mask = PHP_INT_MAX; } do { $rand = hexdec(bin2hex(substr($this->bytes(8), 0, $bytes))); $result = $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); echo('state changed'); return substr($hash, self::STATE_SIZE); } } $prng = new PseudoRandomSequence("test"); $prng->int(0,99);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BMcRN
function name:  (null)
number of ops:  9
compiled vars:  !0 = $prng
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   NEW                                              $1      'PseudoRandomSequence'
          1        SEND_VAL_EX                                              'test'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   53     4        INIT_METHOD_CALL                                         !0, 'int'
          5        SEND_VAL_EX                                              0
          6        SEND_VAL_EX                                              99
          7        DO_FCALL                                      0          
          8      > RETURN                                                   1

Class PseudoRandomSequence:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BMcRN
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 = 43) Position 1 = 38, Position 2 = 39
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 39
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
Branch analysis from position: 39
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/BMcRN
function name:  int
number of ops:  61
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, 1
   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        CAST                                          4  ~26     ~25
         35        ASSIGN                                                   !6, ~26
   25    36        IS_SMALLER                                               !6, 0
         37      > JMPZ                                                     ~28, ->39
   27    38    >   ASSIGN                                                   !6, 9223372036854775807
   30    39    >   INIT_FCALL                                               'hexdec'
         40        INIT_FCALL                                               'bin2hex'
         41        INIT_FCALL                                               'substr'
         42        INIT_METHOD_CALL                                         'bytes'
         43        SEND_VAL_EX                                              8
         44        DO_FCALL                                      0  $30     
         45        SEND_VAR                                                 $30
         46        SEND_VAL                                                 0
         47        SEND_VAR                                                 !5
         48        DO_ICALL                                         $31     
         49        SEND_VAR                                                 $31
         50        DO_ICALL                                         $32     
         51        SEND_VAR                                                 $32
         52        DO_ICALL                                         $33     
         53        ASSIGN                                                   !7, $33
   31    54        BW_AND                                           ~35     !7, !6
         55        ASSIGN                                                   !8, ~35
   32    56        IS_SMALLER                                               !2, !8
         57      > JMPNZ                                                    ~37, ->39
   33    58    >   ADD                                              ~38     !8, !0
         59      > RETURN                                                   ~38
   34    60*     > 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/BMcRN
function name:  bytes
number of ops:  15
compiled vars:  !0 = $n, !1 = $ret
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        ASSIGN                                                   !1, ''
   39     2    >   INIT_METHOD_CALL                                         'mixState'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN_OP                                     8          !1, $3
   40     5        STRLEN                                           ~5      !1
          6        IS_SMALLER                                               ~5, !0
          7      > JMPNZ                                                    ~6, ->2
   41     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !1
         10        SEND_VAL                                                 0
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $7      
         13      > RETURN                                                   $7
   42    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/BMcRN
function name:  mixState
number of ops:  21
compiled vars:  !0 = $hash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     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
   46     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
   47    14        ECHO                                                     'state+changed'
   48    15        INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 32
         18        DO_ICALL                                         $6      
         19      > RETURN                                                   $6
   49    20*     > RETURN                                                   null

End of function mixstate

End of class PseudoRandomSequence.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.38 ms | 1413 KiB | 27 Q