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@0
|
10 * Copyright (c) 2015 - 2017 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@0
|
54 $min = RandomCompat_intval($min);
|
Chris@0
|
55 } catch (TypeError $ex) {
|
Chris@0
|
56 throw new TypeError(
|
Chris@0
|
57 'random_int(): $min must be an integer'
|
Chris@0
|
58 );
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 try {
|
Chris@0
|
62 $max = RandomCompat_intval($max);
|
Chris@0
|
63 } catch (TypeError $ex) {
|
Chris@0
|
64 throw new TypeError(
|
Chris@0
|
65 'random_int(): $max must be an integer'
|
Chris@0
|
66 );
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Now that we've verified our weak typing system has given us an integer,
|
Chris@0
|
71 * let's validate the logic then we can move forward with generating random
|
Chris@0
|
72 * integers along a given range.
|
Chris@0
|
73 */
|
Chris@0
|
74 if ($min > $max) {
|
Chris@0
|
75 throw new Error(
|
Chris@0
|
76 'Minimum value must be less than or equal to the maximum value'
|
Chris@0
|
77 );
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 if ($max === $min) {
|
Chris@12
|
81 return (int) $min;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Initialize variables to 0
|
Chris@0
|
86 *
|
Chris@0
|
87 * We want to store:
|
Chris@0
|
88 * $bytes => the number of random bytes we need
|
Chris@0
|
89 * $mask => an integer bitmask (for use with the &) operator
|
Chris@0
|
90 * so we can minimize the number of discards
|
Chris@0
|
91 */
|
Chris@0
|
92 $attempts = $bits = $bytes = $mask = $valueShift = 0;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * At this point, $range is a positive number greater than 0. It might
|
Chris@0
|
96 * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to
|
Chris@0
|
97 * a float and we will lose some precision.
|
Chris@0
|
98 */
|
Chris@0
|
99 $range = $max - $min;
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Test for integer overflow:
|
Chris@0
|
103 */
|
Chris@0
|
104 if (!is_int($range)) {
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Still safely calculate wider ranges.
|
Chris@0
|
108 * Provided by @CodesInChaos, @oittaa
|
Chris@0
|
109 *
|
Chris@0
|
110 * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
|
Chris@0
|
111 *
|
Chris@0
|
112 * We use ~0 as a mask in this case because it generates all 1s
|
Chris@0
|
113 *
|
Chris@0
|
114 * @ref https://eval.in/400356 (32-bit)
|
Chris@0
|
115 * @ref http://3v4l.org/XX9r5 (64-bit)
|
Chris@0
|
116 */
|
Chris@0
|
117 $bytes = PHP_INT_SIZE;
|
Chris@0
|
118 $mask = ~0;
|
Chris@0
|
119
|
Chris@0
|
120 } else {
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * $bits is effectively ceil(log($range, 2)) without dealing with
|
Chris@0
|
124 * type juggling
|
Chris@0
|
125 */
|
Chris@0
|
126 while ($range > 0) {
|
Chris@0
|
127 if ($bits % 8 === 0) {
|
Chris@0
|
128 ++$bytes;
|
Chris@0
|
129 }
|
Chris@0
|
130 ++$bits;
|
Chris@0
|
131 $range >>= 1;
|
Chris@0
|
132 $mask = $mask << 1 | 1;
|
Chris@0
|
133 }
|
Chris@0
|
134 $valueShift = $min;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 $val = 0;
|
Chris@0
|
138 /**
|
Chris@0
|
139 * Now that we have our parameters set up, let's begin generating
|
Chris@0
|
140 * random integers until one falls between $min and $max
|
Chris@0
|
141 */
|
Chris@0
|
142 do {
|
Chris@0
|
143 /**
|
Chris@0
|
144 * The rejection probability is at most 0.5, so this corresponds
|
Chris@0
|
145 * to a failure probability of 2^-128 for a working RNG
|
Chris@0
|
146 */
|
Chris@0
|
147 if ($attempts > 128) {
|
Chris@0
|
148 throw new Exception(
|
Chris@0
|
149 'random_int: RNG is broken - too many rejections'
|
Chris@0
|
150 );
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Let's grab the necessary number of random bytes
|
Chris@0
|
155 */
|
Chris@0
|
156 $randomByteString = random_bytes($bytes);
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * Let's turn $randomByteString into an integer
|
Chris@0
|
160 *
|
Chris@0
|
161 * This uses bitwise operators (<< and |) to build an integer
|
Chris@0
|
162 * out of the values extracted from ord()
|
Chris@0
|
163 *
|
Chris@0
|
164 * Example: [9F] | [6D] | [32] | [0C] =>
|
Chris@0
|
165 * 159 + 27904 + 3276800 + 201326592 =>
|
Chris@0
|
166 * 204631455
|
Chris@0
|
167 */
|
Chris@0
|
168 $val &= 0;
|
Chris@0
|
169 for ($i = 0; $i < $bytes; ++$i) {
|
Chris@0
|
170 $val |= ord($randomByteString[$i]) << ($i * 8);
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * Apply mask
|
Chris@0
|
175 */
|
Chris@0
|
176 $val &= $mask;
|
Chris@0
|
177 $val += $valueShift;
|
Chris@0
|
178
|
Chris@0
|
179 ++$attempts;
|
Chris@0
|
180 /**
|
Chris@0
|
181 * If $val overflows to a floating point number,
|
Chris@0
|
182 * ... or is larger than $max,
|
Chris@0
|
183 * ... or smaller than $min,
|
Chris@0
|
184 * then try again.
|
Chris@0
|
185 */
|
Chris@0
|
186 } while (!is_int($val) || $val > $max || $val < $min);
|
Chris@0
|
187
|
Chris@12
|
188 return (int) $val;
|
Chris@0
|
189 }
|
Chris@0
|
190 }
|