3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Random_* Compatibility Library * for using the new PHP 7 random_* API in PHP 5 projects * * The MIT License (MIT) * * Copyright (c) 2015 Paragon Initiative Enterprises * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ if (!function_exists('random_int')) { /** * Fetch a random integer between $min and $max inclusive * * @param int $min * @param int $max * * @throws Exception * * @return int */ function random_int($min, $max) { /** * Type and input logic checks */ if (!is_int($min)) { throw new Exception( 'random_int(): $min must be an integer' ); } if (!is_int($max)) { throw new Exception( 'random_int(): $max must be an integer' ); } if ($min > $max) { throw new Exception( 'Minimum value must be less than or equal to the maximum value' ); } if ($max === $min) { return $min; } /** * Initialize variables to 0 * * We want to store: * $bytes => the number of random bytes we need * $mask => an integer bitmask (for use with the &) operator * so we can minimize the number of discards */ $attempts = $bits = $bytes = $mask = $valueShift = 0; /** * At this point, $range is a positive number greater than 0. It might * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to * a float and we will lose some precision. */ $range = $max - $min; /** * Test for integer overflow: */ if (!is_int($range)) { /** * Still safely calculate wider ranges. * Provided by @CodesInChaos, @oittaa * * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 * * We use ~0 as a mask in this case because it generates all 1s * * @ref https://eval.in/400356 (32-bit) * @ref http://3v4l.org/XX9r5 (64-bit) */ $bytes = PHP_INT_SIZE; $mask = ~0; } else { /** * $bits is effectively ceil(log($range, 2)) without dealing with * type juggling */ while ($range > 0) { if ($bits % 8 === 0) { ++$bytes; } ++$bits; $range >>= 1; $mask = $mask << 1 | 1; } $valueShift = $min; } /** * Now that we have our parameters set up, let's begin generating * random integers until one falls between $min and $max */ do { /** * The rejection probability is at most 0.5, so this corresponds * to a failure probability of 2^-128 for a working RNG */ if ($attempts > 128) { throw new Exception( 'random_int: RNG is broken - too many rejections' ); } /** * Let's grab the necessary number of random bytes */ $randomByteString = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); if ($randomByteString === false) { throw new Exception( 'Random number generator failure' ); } /** * Let's turn $randomByteString into an integer * * This uses bitwise operators (<< and |) to build an integer * out of the values extracted from ord() * * Example: [9F] | [6D] | [32] | [0C] => * 159 + 27904 + 3276800 + 201326592 => * 204631455 */ $val = 0; for ($i = 0; $i < $bytes; ++$i) { $val |= ord($randomByteString[$i]) << ($i * 8); } /** * Apply mask */ $val &= $mask; $val += $valueShift; ++$attempts; /** * If $val overflows to a floating point number, * ... or is larger than $max, * ... or smaller than $int, * then try again. */ } while (!is_int($val) || $val > $max || $val < $min); return (int) $val; } } function random_unique_select($num, array $possible_values) { $sizeof = count($possible_values); if ($num > $sizeof) { throw new InvalidArgumentException('$num is too large'); } $selected = []; for ($i = 0; $i < $num; ++$i) { $r = random_int(0, $sizeof - 1); $selected[] = $possible_values[$r]; unset($possible_values[$r]); --$sizeof; $possible_values = array_values($possible_values); } return $selected; } $lottery = random_unique_select(6, range(1,29)); var_dump($lottery);

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)
5.6.120.0130.00014.25
5.6.110.0070.00814.40
5.6.100.0030.01414.16
5.6.90.0060.00914.17
5.6.80.0120.00314.34
5.6.70.0040.01114.33
5.6.60.0120.00314.15
5.6.50.0070.00714.12
5.6.40.0030.01214.16
5.6.30.0000.01514.35
5.6.20.0070.00714.27
5.6.10.0030.01414.06
5.6.00.0040.00814.43
5.5.280.0060.00611.19
5.5.270.0070.00311.14
5.5.260.0080.00411.09
5.5.250.0040.00811.02
5.5.240.0090.00311.23
5.5.230.0060.00611.10
5.5.220.0030.00611.16
5.5.210.0080.00311.06
5.5.200.0060.00311.39
5.5.190.0070.00310.97
5.5.180.0090.00311.15
5.5.170.0060.01014.30
5.5.160.0110.00011.32
5.5.150.0040.00710.83
5.5.140.0030.00611.10
5.5.130.0000.01010.82
5.5.120.0000.01111.19
5.5.110.0040.00811.27
5.5.100.0090.00311.20
5.5.90.0030.01011.37
5.5.80.0000.01010.99
5.5.70.0060.00711.28
5.5.60.0060.00611.19
5.5.50.0090.00311.25
5.5.40.0090.00011.28
5.5.30.0060.00611.23
5.5.20.0060.00311.28
5.5.10.0070.00610.64
5.5.00.0040.00710.81
5.4.440.0070.00411.23
5.4.430.0070.00410.90
5.4.420.0080.00311.20
5.4.410.0060.00311.14
5.4.400.0060.00411.07
5.4.390.0110.00011.01
5.4.380.0040.00710.78
5.4.370.0030.01011.02
5.4.360.0030.00910.92
5.4.350.0060.00610.75
5.4.340.0050.00511.26
5.4.330.0040.00810.95
5.4.320.0060.00610.78
5.4.310.0060.00611.14
5.4.300.0070.00411.17
5.4.290.0110.00411.23
5.4.280.0080.00311.32
5.4.270.0000.01110.98
5.4.260.0030.00711.04
5.4.250.0030.00911.09
5.4.240.0070.00411.02
5.4.230.0030.00611.34
5.4.220.0050.00611.15
5.4.210.0060.00311.16
5.4.200.0030.00711.02
5.4.190.0000.01111.08
5.4.180.0100.00011.03
5.4.170.0040.00411.05
5.4.160.0000.01111.02
5.4.150.0040.00811.32
5.4.140.0000.00911.08
5.4.130.0060.00311.13
5.4.120.0040.00811.06
5.4.110.0060.00310.87
5.4.100.0030.00611.07
5.4.90.0030.00710.92
5.4.80.0120.00010.91
5.4.70.0100.00011.06
5.4.60.0070.00211.16
5.4.50.0050.00611.02
5.4.40.0000.01011.17
5.4.30.0030.00711.02
5.4.20.0050.00511.09
5.4.10.0090.00010.73
5.4.00.0120.00011.11
5.3.290.0030.00610.09
5.3.280.0030.00610.03
5.3.270.0030.00510.31
5.3.260.0050.00310.27
5.3.250.0060.00310.29
5.3.240.0040.00410.14
5.3.230.0070.00310.14
5.3.220.0040.00410.21
5.3.210.0090.00310.20
5.3.200.0080.00310.21
5.3.190.0070.00310.28
5.3.180.0050.00310.45
5.3.170.0030.00710.48
5.3.160.0030.00710.51
5.3.150.0040.00410.50
5.3.140.0030.00610.33
5.3.130.0060.00510.15
5.3.120.0110.00010.25
5.3.110.0040.00710.37
5.3.100.0030.00610.38
5.3.90.0000.00810.48
5.3.80.0080.00210.27
5.3.70.0030.00610.30
5.3.60.0050.00510.20
5.3.50.0000.00910.32
5.3.40.0030.00710.07
5.3.30.0070.00010.07
5.3.20.0090.00310.09
5.3.10.0050.0059.84
5.3.00.0100.00010.11
5.2.170.0060.0039.28
5.2.160.0100.0009.47
5.2.150.0090.0009.24
5.2.140.0050.0039.34
5.2.130.0000.0099.29
5.2.120.0060.0039.32
5.2.110.0000.0099.33
5.2.100.0050.0049.35
5.2.90.0000.0089.05
5.2.80.0030.0039.26
5.2.70.0060.0039.25
5.2.60.0050.0058.93
5.2.50.0060.0039.32
5.2.40.0030.0069.27
5.2.30.0050.0029.29
5.2.20.0030.0069.06
5.2.10.0060.0038.99
5.2.00.0000.0088.98
5.1.60.0050.0038.84
5.1.50.0000.0088.84
5.1.40.0040.0048.84
5.1.30.0090.0008.84
5.1.20.0000.0068.84
5.1.10.0000.0098.84
5.1.00.0000.0078.84
5.0.50.0030.0038.84
5.0.40.0060.0008.84
5.0.30.0000.0058.84
5.0.20.0040.0048.84
5.0.10.0000.0068.84
5.0.00.0040.0038.84
4.4.90.0070.0008.84
4.4.80.0060.0008.84
4.4.70.0030.0038.84
4.4.60.0000.0058.84
4.4.50.0030.0038.84
4.4.40.0030.0038.84
4.4.30.0000.0068.84
4.4.20.0040.0008.84
4.4.10.0000.0058.84
4.4.00.0000.0058.84
4.3.110.0030.0038.84
4.3.100.0030.0038.84
4.3.90.0040.0008.84
4.3.80.0060.0008.84
4.3.70.0000.0078.84
4.3.60.0040.0028.84
4.3.50.0030.0038.84
4.3.40.0000.0068.84
4.3.30.0030.0038.84
4.3.20.0000.0068.84
4.3.10.0020.0048.84
4.3.00.0030.0038.84

preferences:
41.19 ms | 403 KiB | 5 Q