1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Savvot\Random; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Pure PHP implementation of Mersenne twister PRNG (builtin mt_rand() function) |
7
|
|
|
* This generator is fully compatible with mt_rand() so can be used as replacement |
8
|
|
|
* Random sequences from both methods with same int seed will match perfectly |
9
|
|
|
* |
10
|
|
|
* WARNING: This generator is compatible with mt_rand ONLY when int seed is specified |
11
|
|
|
* In case of string seed (even if it is numeric) or empty (default) seed, results will be different |
12
|
|
|
* |
13
|
|
|
* Based on PHP source code from https://github.com/php/php-src/blob/master/ext/standard/rand.c |
14
|
|
|
* |
15
|
|
|
* @package Savvot\Random |
16
|
|
|
* @author SavvoT <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class MtRand extends AbstractRand |
19
|
|
|
{ |
20
|
|
|
// PHP builtin mt_rand returns 31bit integers |
21
|
|
|
const INT_MAX = 0x7fffffff; |
22
|
|
|
|
23
|
|
|
const N = 624; |
24
|
|
|
const M = 397; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function randomInt() |
30
|
|
|
{ |
31
|
|
|
if ($this->state['left'] === 0) { |
32
|
|
|
$this->reload(); |
33
|
|
|
} |
34
|
|
|
$this->state['left']--; |
35
|
|
|
|
36
|
|
|
$s1 = $this->state['mt'][$this->state['next']]; |
37
|
|
|
$this->state['next'] = (++$this->state['next']) % self::N; |
38
|
|
|
|
39
|
|
|
$s1 ^= ($s1 >> 11); |
40
|
|
|
$s1 ^= ($s1 << 7) & 0x9d2c5680; |
41
|
|
|
$s1 ^= ($s1 << 15) & 0xefc60000; |
42
|
|
|
|
43
|
|
|
return ($s1 ^ ($s1 >> 18)) >> 1; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
protected function init() |
50
|
|
|
{ |
51
|
|
|
$state = []; |
52
|
|
|
if (is_int($this->seed)) { |
53
|
|
|
// For int seed we must repeat mt_rand initialization |
54
|
|
|
$state[0] = $this->seed & 0xffffffff; |
55
|
|
|
} else { |
56
|
|
|
$st = unpack('V*', $this->hashedSeed); |
57
|
|
|
$state[0] = $st[1] ^ $st[2] ^ $st[3] ^ $st[4]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
for ($i = 1; $i < self::N; $i++) { |
61
|
|
|
$r = $state[$i - 1]; |
62
|
|
|
$state[$i] = (1812433253 * ($r ^ ($r >> 30)) + $i) & 0xffffffff; |
63
|
|
|
} |
64
|
|
|
$this->state = ['mt' => $state]; |
65
|
|
|
$this->reload(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* "Twist" stage of algorithm |
70
|
|
|
*/ |
71
|
|
|
private function reload() |
72
|
|
|
{ |
73
|
|
|
$p = 0; |
74
|
|
View Code Duplication |
for ($i = self::N - self::M; $i--; ++$p) { |
|
|
|
|
75
|
|
|
$m = $this->state['mt'][$p + self::M]; |
76
|
|
|
$u = $this->state['mt'][$p]; |
77
|
|
|
$v = $this->state['mt'][$p + 1]; |
78
|
|
|
$this->state['mt'][$p] = ($m ^ ((($u & 0x80000000) | ($v & 0x7fffffff)) >> 1) ^ (-($u & 0x00000001) & 0x9908b0df)); |
79
|
|
|
} |
80
|
|
View Code Duplication |
for ($i = self::M; --$i; ++$p) { |
|
|
|
|
81
|
|
|
$m = $this->state['mt'][$p + self::M - self::N]; |
82
|
|
|
$u = $this->state['mt'][$p]; |
83
|
|
|
$v = $this->state['mt'][$p + 1]; |
84
|
|
|
$this->state['mt'][$p] = ($m ^ ((($u & 0x80000000) | ($v & 0x7fffffff)) >> 1) ^ (-($u & 0x00000001) & 0x9908b0df)); |
85
|
|
|
} |
86
|
|
|
$m = $this->state['mt'][$p + self::M - self::N]; |
87
|
|
|
$u = $this->state['mt'][$p]; |
88
|
|
|
$v = $this->state['mt'][0]; |
89
|
|
|
$this->state['mt'][$p] = ($m ^ ((($u & 0x80000000) | ($v & 0x7fffffff)) >> 1) ^ (-($u & 0x00000001) & 0x9908b0df)); |
90
|
|
|
|
91
|
|
|
$this->state['left'] = self::N; |
92
|
|
|
$this->state['next'] = 0; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.