3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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()); }
based on tviGU
Output for 5.1.2 - 7.2.0
int(589729474836275200) int(1099965269872060878) int(4600125106130094080) int(2009783703034501760) int(3472677648056942592) int(1577744694863021373) int(3484804279946948778) int(2694961782500509696) int(2543321254451888878) int(2200041835931590656) int(1446339994242361344) int(2518523471968858112) int(2211616845967552954) int(3717443101740859392) int(245052585091773527) int(4094952377526935900) int(2513311962105084906) int(2603169904933447680) int(1189907916478496768) int(3166581282181126144) int(4207794694136153498) int(4073351267802634240) int(1743380542910582784) int(750036784047443968) int(2084646099667275776) int(2000998293962491094) int(532129525491406976) int(398653521312129024) int(3820640171256279119) int(2130408984675541856) int(1134241219800119296) int(78625717825533952) int(1269235890366190858) int(4412837160375578624) int(2717540130560379495) int(4495389478755876864) int(4606964540811705271) int(2462418484141444891) int(1259962477923706880) int(691673641080519076) int(4353942054477503680) int(4016918982709636617) int(4371789790571888359) int(2019182971371352064) int(119225226642966528) int(2386900553748546801) int(772928990137597952) int(3536281087152821470) int(1972356278327691991) int(4585772548246755328) int(3207412515672469504) int(3431586924077657582) int(4353118978375757105) int(382584382399735808) int(2666021735500767232) int(2077572489612971762) int(1268588082999541760) int(1789811177239400878) int(1235345463525228544) int(1322228300822974464) int(4104248648932534104) int(569671577444681728) int(4501940844769896448) int(4009798593770427674) int(285136903446697984) int(1475700306173313024) int(4325875986006988236) int(644478231907913728) int(3219025012110174445) int(3013055943584133120) int(3232339765996558336) int(3151461919735494656) int(3727853855875142335) int(820657289183909888) int(635439280913127424) int(2525141178430839702) int(3426333340104701470) int(1620115747031091200) int(4413846783397873334) int(1136936877896392397) int(3494412524422766562) int(253729720579093427) int(1562677644613605376) int(3534075962507419648) int(1443502578051964928) int(3048319316328847360) int(1071139201880907776) int(3181813766780209152) int(3368336423946633216) int(3762715410118834176) int(1000931979331555973) int(1248391809507696760) int(4464015471160071353) int(4303175425761009664) int(4273207685769958970) int(3647810734656649216) int(3731073700865003520) int(3994483117144055808) int(4307268801958440323) int(2226313902547415040)
Output for 5.0.0 - 5.1.1
Fatal error: Call to undefined function hash() in /in/rgcTO on line 41
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rgcTO on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rgcTO on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/rgcTO on line 4
Process exited with code 255.