Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 if (!is_callable('random_int')) {
|
Chris@0
|
4 /**
|
Chris@0
|
5 * Random_* Compatibility Library
|
Chris@0
|
6 * for using the new PHP 7 random_* API in PHP 5 projects
|
Chris@0
|
7 *
|
Chris@0
|
8 * The MIT License (MIT)
|
Chris@0
|
9 *
|
Chris@16
|
10 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
|
Chris@0
|
11 *
|
Chris@0
|
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@0
|
13 * of this software and associated documentation files (the "Software"), to deal
|
Chris@0
|
14 * in the Software without restriction, including without limitation the rights
|
Chris@0
|
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@0
|
16 * copies of the Software, and to permit persons to whom the Software is
|
Chris@0
|
17 * furnished to do so, subject to the following conditions:
|
Chris@0
|
18 *
|
Chris@0
|
19 * The above copyright notice and this permission notice shall be included in
|
Chris@0
|
20 * all copies or substantial portions of the Software.
|
Chris@0
|
21 *
|
Chris@0
|
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@0
|
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@0
|
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@0
|
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@0
|
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@0
|
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
Chris@0
|
28 * SOFTWARE.
|
Chris@0
|
29 */
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Fetch a random integer between $min and $max inclusive
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param int $min
|
Chris@0
|
35 * @param int $max
|
Chris@0
|
36 *
|
Chris@0
|
37 * @throws Exception
|
Chris@0
|
38 *
|
Chris@0
|
39 * @return int
|
Chris@0
|
40 */
|
Chris@0
|
41 function random_int($min, $max)
|
Chris@0
|
42 {
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Type and input logic checks
|
Chris@0
|
45 *
|
Chris@0
|
46 * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
|
Chris@0
|
47 * (non-inclusive), it will sanely cast it to an int. If you it's equal to
|
Chris@0
|
48 * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
|
Chris@0
|
49 * lose precision, so the <= and => operators might accidentally let a float
|
Chris@0
|
50 * through.
|
Chris@0
|
51 */
|
Chris@0
|
52
|
Chris@0
|
53 try {
|
Chris@16
|
54 /** @var int $min */
|
Chris@0
|
55 $min = RandomCompat_intval($min);
|
Chris@0
|
56 } catch (TypeError $ex) {
|
Chris@0
|
57 throw new TypeError(
|
Chris@0
|
58 'random_int(): $min must be an integer'
|
Chris@0
|
59 );
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 try {
|
Chris@16
|
63 /** @var int $max */
|
Chris@0
|
64 $max = RandomCompat_intval($max);
|
Chris@0
|
65 } catch (TypeError $ex) {
|
Chris@0
|
66 throw new TypeError(
|
Chris@0
|
67 'random_int(): $max must be an integer'
|
Chris@0
|
68 );
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Now that we've verified our weak typing system has given us an integer,
|
Chris@0
|
73 * let's validate the logic then we can move forward with generating random
|
Chris@0
|
74 * integers along a given range.
|
Chris@0
|
75 */
|
Chris@0
|
76 if ($min > $max) {
|
Chris@0
|
77 throw new Error(
|
Chris@0
|
78 'Minimum value must be less than or equal to the maximum value'
|
Chris@0
|
79 );
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if ($max === $min) {
|
Chris@12
|
83 return (int) $min;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Initialize variables to 0
|
Chris@0
|
88 *
|
Chris@0
|
89 * We want to store:
|
Chris@0
|
90 * $bytes => the number of random bytes we need
|
Chris@0
|
91 * $mask => an integer bitmask (for use with the &) operator
|
Chris@0
|
92 * so we can minimize the number of discards
|
Chris@0
|
93 */
|
Chris@0
|
94 $attempts = $bits = $bytes = $mask = $valueShift = 0;
|
Chris@16
|
95 /** @var int $attempts */
|
Chris@16
|
96 /** @var int $bits */
|
Chris@16
|
97 /** @var int $bytes */
|
Chris@16
|
98 /** @var int $mask */
|
Chris@16
|
99 /** @var int $valueShift */
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * At this point, $range is a positive number greater than 0. It might
|
Chris@0
|
103 * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to
|
Chris@0
|
104 * a float and we will lose some precision.
|
Chris@16
|
105 *
|
Chris@16
|
106 * @var int|float $range
|
Chris@0
|
107 */
|
Chris@0
|
108 $range = $max - $min;
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Test for integer overflow:
|
Chris@0
|
112 */
|
Chris@0
|
113 if (!is_int($range)) {
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Still safely calculate wider ranges.
|
Chris@0
|
117 * Provided by @CodesInChaos, @oittaa
|
Chris@0
|
118 *
|
Chris@0
|
119 * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
|
Chris@0
|
120 *
|
Chris@0
|
121 * We use ~0 as a mask in this case because it generates all 1s
|
Chris@0
|
122 *
|
Chris@0
|
123 * @ref https://eval.in/400356 (32-bit)
|
Chris@0
|
124 * @ref http://3v4l.org/XX9r5 (64-bit)
|
Chris@0
|
125 */
|
Chris@0
|
126 $bytes = PHP_INT_SIZE;
|
Chris@16
|
127 /** @var int $mask */
|
Chris@0
|
128 $mask = ~0;
|
Chris@0
|
129
|
Chris@0
|
130 } else {
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * $bits is effectively ceil(log($range, 2)) without dealing with
|
Chris@0
|
134 * type juggling
|
Chris@0
|
135 */
|
Chris@0
|
136 while ($range > 0) {
|
Chris@0
|
137 if ($bits % 8 === 0) {
|
Chris@0
|
138 ++$bytes;
|
Chris@0
|
139 }
|
Chris@0
|
140 ++$bits;
|
Chris@0
|
141 $range >>= 1;
|
Chris@16
|
142 /** @var int $mask */
|
Chris@0
|
143 $mask = $mask << 1 | 1;
|
Chris@0
|
144 }
|
Chris@0
|
145 $valueShift = $min;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@16
|
148 /** @var int $val */
|
Chris@0
|
149 $val = 0;
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Now that we have our parameters set up, let's begin generating
|
Chris@0
|
152 * random integers until one falls between $min and $max
|
Chris@0
|
153 */
|
Chris@16
|
154 /** @psalm-suppress RedundantCondition */
|
Chris@0
|
155 do {
|
Chris@0
|
156 /**
|
Chris@0
|
157 * The rejection probability is at most 0.5, so this corresponds
|
Chris@0
|
158 * to a failure probability of 2^-128 for a working RNG
|
Chris@0
|
159 */
|
Chris@0
|
160 if ($attempts > 128) {
|
Chris@0
|
161 throw new Exception(
|
Chris@0
|
162 'random_int: RNG is broken - too many rejections'
|
Chris@0
|
163 );
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 /**
|
Chris@0
|
167 * Let's grab the necessary number of random bytes
|
Chris@0
|
168 */
|
Chris@0
|
169 $randomByteString = random_bytes($bytes);
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Let's turn $randomByteString into an integer
|
Chris@0
|
173 *
|
Chris@0
|
174 * This uses bitwise operators (<< and |) to build an integer
|
Chris@0
|
175 * out of the values extracted from ord()
|
Chris@0
|
176 *
|
Chris@0
|
177 * Example: [9F] | [6D] | [32] | [0C] =>
|
Chris@0
|
178 * 159 + 27904 + 3276800 + 201326592 =>
|
Chris@0
|
179 * 204631455
|
Chris@0
|
180 */
|
Chris@0
|
181 $val &= 0;
|
Chris@0
|
182 for ($i = 0; $i < $bytes; ++$i) {
|
Chris@0
|
183 $val |= ord($randomByteString[$i]) << ($i * 8);
|
Chris@0
|
184 }
|
Chris@16
|
185 /** @var int $val */
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * Apply mask
|
Chris@0
|
189 */
|
Chris@0
|
190 $val &= $mask;
|
Chris@0
|
191 $val += $valueShift;
|
Chris@0
|
192
|
Chris@0
|
193 ++$attempts;
|
Chris@0
|
194 /**
|
Chris@0
|
195 * If $val overflows to a floating point number,
|
Chris@0
|
196 * ... or is larger than $max,
|
Chris@0
|
197 * ... or smaller than $min,
|
Chris@0
|
198 * then try again.
|
Chris@0
|
199 */
|
Chris@0
|
200 } while (!is_int($val) || $val > $max || $val < $min);
|
Chris@0
|
201
|
Chris@12
|
202 return (int) $val;
|
Chris@0
|
203 }
|
Chris@0
|
204 }
|