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($this->state, 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));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0131.99118.43
8.3.50.0131.98822.04
8.3.40.0101.99318.81
8.3.30.0032.00019.09
8.3.20.0071.99420.35
8.3.10.0032.00021.85
8.3.00.0031.99919.36
8.2.180.0032.00118.29
8.2.170.0071.99722.96
8.2.160.0071.99822.08
8.2.150.0071.99724.18
8.2.140.0031.99924.66
8.2.130.0032.00026.16
8.2.120.0002.00220.73
8.2.110.0032.00019.31
8.2.100.0002.00417.63
8.2.90.0031.99819.40
8.2.80.0002.00517.97
8.2.70.0071.99517.75
8.2.60.0031.99718.16
8.2.50.0071.99718.07
8.2.40.0031.99818.28
8.2.30.0101.98918.32
8.2.20.0031.99817.87
8.2.10.0032.00018.32
8.2.00.0071.99317.89
8.1.280.0071.99725.92
8.1.270.0002.00320.66
8.1.260.0071.99726.35
8.1.250.0032.00128.09
8.1.240.0071.99424.05
8.1.230.0032.00117.61
8.1.220.0031.99917.77
8.1.210.0031.99918.77
8.1.200.0071.99517.47
8.1.190.0071.99617.61
8.1.180.0071.99618.10
8.1.170.0071.99420.42
8.1.160.0032.00022.16
8.1.150.0031.99719.04
8.1.140.0002.00217.69
8.1.130.0071.99417.97
8.1.120.0002.00017.70
8.1.110.0031.99917.66
8.1.100.0032.00017.61
8.1.90.0031.99817.67
8.1.80.0031.99817.54
8.1.70.0031.99917.57
8.1.60.0032.00017.66
8.1.50.0101.99117.56
8.1.40.0031.99917.70
8.1.30.0032.00017.80
8.1.20.0031.99817.68
8.1.10.0032.00017.75
8.1.00.0032.00017.72
8.0.300.0031.99918.77
8.0.290.0032.00516.75
8.0.280.0031.99818.51
8.0.270.0101.99117.25
8.0.260.0002.00717.23
8.0.250.0071.99416.94
8.0.240.0002.00217.09
8.0.230.0002.00216.88
8.0.220.0002.00417.00
8.0.210.0032.00016.97
8.0.200.0271.98617.00
8.0.190.0002.00416.83
8.0.180.0031.99917.00
8.0.170.0032.00116.88
8.0.160.0031.99916.86
8.0.150.0032.00016.92
8.0.140.0002.00416.94
8.0.130.0821.91113.49
8.0.120.0071.99716.89
8.0.110.0002.00316.93
8.0.100.0002.00216.79
8.0.90.0071.99717.01
8.0.80.0191.97816.93
8.0.70.0031.99816.93
8.0.60.0002.00416.79
8.0.50.0131.98916.75
8.0.30.0161.96517.09
8.0.20.0052.00017.40
8.0.10.0002.00317.09
8.0.00.0101.99016.67
7.4.330.0061.99415.03
7.4.320.0002.00216.40
7.4.300.0002.00416.39
7.4.290.0071.99416.53
7.4.280.0031.99816.41
7.4.270.0101.99116.53
7.4.260.0031.99716.46
7.4.250.0031.99916.60
7.4.240.0031.99916.46
7.4.230.0002.00316.59
7.4.220.0031.99816.38
7.4.210.0071.99616.42
7.4.200.0031.99916.54
7.4.160.0201.98616.46
7.4.150.0231.98817.40
7.4.140.0241.97417.86
7.4.130.0182.00116.64
7.4.120.0111.98216.46
7.4.110.0102.39616.42
7.4.100.0132.43516.55
7.4.90.0162.46216.49
7.4.80.0102.45019.39
7.4.70.0162.45516.75
7.4.60.0032.44916.74
7.4.50.0002.49916.19
7.4.40.0262.39816.51
7.4.30.0132.42516.50
7.4.00.0102.48614.98
7.3.330.0421.93913.43
7.3.320.0171.98813.43
7.3.310.0032.00016.49
7.3.300.0071.99616.39
7.3.290.0042.00016.27
7.3.280.0101.98516.39
7.3.270.0071.99217.40
7.3.260.0191.97416.61
7.3.250.0151.99216.58
7.3.240.0131.99516.36
7.3.230.0132.38016.59
7.3.210.0162.41116.41
7.3.200.0162.43019.39
7.3.190.0132.44916.43
7.3.180.0162.46316.38
7.3.170.0072.45016.50
7.3.160.0162.39816.35
7.2.330.0072.44716.91
7.2.320.0202.44116.85
7.2.310.0062.46716.59
7.2.300.0162.42216.84
7.2.290.0132.42216.84
7.2.60.0102.48316.73
7.2.00.0032.49819.20
7.1.200.0072.48615.70
7.1.100.0032.49818.02
7.1.70.0002.50117.00
7.1.60.0132.48819.50
7.1.50.0072.49517.24
7.1.00.0032.49722.21
7.0.200.0632.36214.60
7.0.140.0102.49022.21
7.0.110.0832.41719.98
7.0.100.0302.47019.99
7.0.90.0532.35719.98
7.0.80.0102.49020.10
7.0.70.0402.46020.11
7.0.60.0672.41720.02
7.0.50.0402.46022.20
7.0.40.0132.47320.09
7.0.30.0072.47020.02
7.0.20.0102.46720.16
7.0.10.0102.46320.14
7.0.00.0072.46720.12
5.6.280.0102.49021.13
5.6.260.0032.49720.71
5.6.250.0072.49320.64
5.6.240.0032.45020.71
5.6.230.0032.47720.48
5.6.220.0102.49020.70
5.6.210.0002.50020.82
5.6.200.0072.40321.09
5.6.190.0102.45321.12
5.6.180.0032.47320.95
5.6.170.0072.45721.30
5.6.160.0032.49720.97
5.6.150.0032.49721.30
5.6.140.0002.45021.06
5.6.130.0132.45721.20
5.6.120.0102.47021.16
5.6.110.0002.50021.07
5.6.100.0072.48021.02
5.6.90.0102.49021.06
5.6.80.0102.47720.31
5.6.70.0102.49020.52
5.6.60.0102.49020.43
5.6.50.0072.48020.34
5.6.40.0002.50020.44
5.6.30.0032.48720.51
5.6.20.0032.49720.56
5.6.10.0002.48320.66
5.6.00.0072.46720.43
5.5.380.0032.45020.30
5.5.370.0132.48720.54
5.5.360.0172.48320.54
5.5.350.0132.47720.17
5.5.340.0032.47020.91
5.5.330.0132.48020.96
5.5.320.0132.45320.88
5.5.310.0102.46720.96
5.5.300.0202.46020.79
5.5.290.0032.47321.05
5.5.280.0032.48720.94
5.5.270.0002.46320.95
5.5.260.0072.47320.96
5.5.250.0032.49720.71
5.5.240.0002.50020.38
5.5.230.0072.49320.13
5.5.220.0002.50020.29
5.5.210.0132.47320.26
5.5.200.0072.49320.18
5.5.190.0102.45720.25
5.5.180.0072.47720.35
5.5.160.0072.47020.39
5.5.150.0132.47020.26
5.5.140.0032.45320.06
5.5.130.0072.45720.18
5.5.120.0172.38020.29
5.5.110.0032.47720.20
5.5.100.0002.50020.16
5.5.90.0102.46720.27
5.5.80.0072.44020.09
5.5.70.0102.44020.15
5.5.60.0132.45319.98
5.5.50.0102.45020.23
5.5.40.0132.45020.00
5.5.30.0072.49320.01
5.5.20.0032.48020.00
5.5.10.0002.48020.14
5.5.00.0072.48020.23
5.4.450.0002.48019.43
5.4.440.0102.49019.35
5.4.430.0032.49719.37
5.4.420.0072.49319.35
5.4.410.0072.49319.13
5.4.400.0102.49019.14
5.4.390.0032.49718.89
5.4.380.0102.49019.13
5.4.370.0072.49318.89
5.4.360.0102.49019.12
5.4.350.0072.49318.98
5.4.340.0072.49319.01
5.4.320.0172.48319.00
5.4.310.0032.49719.11
5.4.300.0002.50019.20
5.4.290.0172.48318.92
5.4.280.0032.49718.98
5.4.270.0072.49318.95
5.4.260.0072.49319.14
5.4.250.0102.49019.11
5.4.240.0132.48719.02
5.4.230.0102.49019.02
5.4.220.0072.49319.00
5.4.210.0072.49319.11
5.4.200.0032.49718.90
5.4.190.0072.49319.08
5.4.180.0002.50019.10
5.4.170.0032.49719.29
5.4.160.0102.49019.00
5.4.150.0102.49018.93
5.4.140.0102.42716.36
5.4.130.3232.17316.34
5.4.120.0072.49316.42
5.4.110.0072.49316.50
5.4.100.0032.49716.31
5.4.90.0072.49316.47
5.4.80.0132.48716.50
5.4.70.0072.49316.39
5.4.60.0102.49016.47
5.4.50.0132.48716.27
5.4.40.0132.48716.46
5.4.30.0032.49716.36
5.4.20.0072.49316.46
5.4.10.0102.49016.40
5.4.00.0002.50015.92

preferences:
39.69 ms | 401 KiB | 5 Q