22
22
* A Mersenne Twister subclass which generates the same numbers as the Python 3 implementation.
23
23
* Source code uses as reference is part of the files:
24
24
* <ul>
25
- * <li><a href="http://svn.python.org/projects/python/trunk/Modules/_randommodule.c">_randommodule.c</a>.</li>
26
- * <li><a href="https://svn.python.org/projects/python/trunk/Lib/random.py">random.py</a>.</li>
25
+ * <li><a href="https://github.com/python/cpython/blob/master/Modules/_randommodule.c">_randommodule.c</a>.</li>
26
+ * <li><a href="https://github.com/python/cpython/blob/master/Lib/random.py">random.py</a>.</li>
27
+ * <li><a href="https://github.com/python/cpython/blob/master/Lib/test/test_random.py">test_random.py</a>.</li>
27
28
* </ul>
29
+ * Test
28
30
*/
29
31
public class MersenneTwisterPy3k extends ReversibleMersenneTwister {
30
32
@@ -92,28 +94,19 @@ public void setSeed(long seed) {
92
94
if (high == 0 ) {
93
95
setSeed (new int []{(int ) seed });
94
96
} else {
95
- setSeed (new int []{high , (int ) (seed & 0xFFFFFFFFL )});
97
+ setSeed (new int []{(int ) (seed & 0xFFFFFFFFL ), high });
96
98
}
97
99
}
98
100
99
- /**
100
- * {@inheritDoc}
101
- * <p>
102
- * Reverses the bytes in the {@code seed} array and calls{@code super.setSeed(int[])}.
103
- * Python Mersenne Twister uses a different byte ordering for the {@code seed} array.
104
- */
105
- @ Override
106
- public void setSeed (int [] seed ) {
107
- // for python compatibility where the seed is a number (big integer)
108
- // and it is big endian
109
- super .setSeed (reverseArray (seed ));
110
- }
111
-
112
101
/**
113
102
* {@inheritDoc}
114
103
* <p>
115
104
* Source code: {@code random_random} method in the file
116
- * <a href="http://svn.python.org/projects/python/trunk/Modules/_randommodule.c">_randommodule.c</a>.
105
+ * <a href="https://github.com/python/cpython/blob/master/Modules/_randommodule.c">_randommodule.c</a>.
106
+ * <p>
107
+ * This method is the same as {@code genrand_res53} method in the
108
+ * <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c">original implementation</a>
109
+ * of the Mersenne Twister.
117
110
*/
118
111
@ Override
119
112
public double nextDouble () {
@@ -124,7 +117,7 @@ public double nextDouble() {
124
117
* {@inheritDoc}
125
118
* <p>
126
119
* Source code: {@code random_random} method in the file
127
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
120
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
128
121
*/
129
122
@ Override
130
123
public double prevDouble () {
@@ -135,6 +128,13 @@ public double prevDouble() {
135
128
* {@inheritDoc}
136
129
* <p>
137
130
* It simply calls {@link #nextDouble()} and does a cast.
131
+ * <p>
132
+ * Source code: {@code random_random} method in the file
133
+ * <a href="https://github.com/python/cpython/blob/master/Modules/_randommodule.c">_randommodule.c</a>.
134
+ * <p>
135
+ * This method is the same as {@code genrand_res53} method in the
136
+ * <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c">original implementation</a>
137
+ * of the Mersenne Twister.
138
138
*/
139
139
@ Override
140
140
public float nextFloat () {
@@ -155,7 +155,7 @@ public float prevFloat() {
155
155
* {@inheritDoc}
156
156
* <p>
157
157
* Source code: {@code _randbelow} method in the file
158
- * <a href="https://svn. python.org/projects/python/trunk /Lib/random.py">random.py</a>.
158
+ * <a href="https://github.com/ python/cpython/blob/master /Lib/random.py">random.py</a>.
159
159
*/
160
160
@ Override
161
161
public int nextInt (int n ) throws IllegalArgumentException {
@@ -174,7 +174,7 @@ public int nextInt(int n) throws IllegalArgumentException {
174
174
* {@inheritDoc}
175
175
* <p>
176
176
* Source code: {@code _randbelow} method in the file
177
- * <a href="https://svn. python.org/projects/python/trunk /Lib/random.py">random.py</a>.
177
+ * <a href="https://github.com/ python/cpython/blob/master /Lib/random.py">random.py</a>.
178
178
*/
179
179
@ Override
180
180
public int prevInt (int n ) throws IllegalArgumentException {
@@ -196,7 +196,7 @@ public int prevInt(int n) throws IllegalArgumentException {
196
196
* when called for 8 bytes.
197
197
* <p>
198
198
* Source code: {@code random_getrandbits} method in the file
199
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
199
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
200
200
*/
201
201
@ Override
202
202
public long nextLong () {
@@ -212,7 +212,7 @@ public long nextLong() {
212
212
* when called for 8 bytes.
213
213
* <p>
214
214
* Source code: {@code random_getrandbits} method in the file
215
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
215
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
216
216
*/
217
217
@ Override
218
218
public long prevLong () {
@@ -225,7 +225,7 @@ public long prevLong() {
225
225
* {@inheritDoc}
226
226
* <p>
227
227
* Source code: {@code random_getrandbits} method in the file
228
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
228
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
229
229
*/
230
230
@ Override
231
231
public void nextBytes (byte [] bytes , int start , int len ) {
@@ -243,7 +243,7 @@ public void nextBytes(byte[] bytes, int start, int len) {
243
243
* {@inheritDoc}
244
244
* <p>
245
245
* Source code: {@code random_getrandbits} method in the file
246
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
246
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
247
247
*/
248
248
@ Override
249
249
public void nextBytes (byte [] bytes ) {
@@ -275,7 +275,7 @@ private void nextBytesFill(byte[] bytes, int start, int len) {
275
275
* {@inheritDoc}
276
276
* <p>
277
277
* Source code: {@code random_getrandbits} method in the file
278
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
278
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
279
279
*/
280
280
@ Override
281
281
public void prevBytes (byte [] bytes ) {
@@ -286,7 +286,7 @@ public void prevBytes(byte[] bytes) {
286
286
* {@inheritDoc}
287
287
* <p>
288
288
* Source code: {@code random_getrandbits} method in the file
289
- * <a href="http ://svn. python.org/projects/python/trunk /Modules/_randommodule.c">_randommodule.c</a>.
289
+ * <a href="https ://github.com/ python/cpython/blob/master /Modules/_randommodule.c">_randommodule.c</a>.
290
290
*/
291
291
@ Override
292
292
public void prevBytes (byte [] bytes , int start , int len ) {
@@ -328,7 +328,7 @@ private void prevBytesFill(byte[] bytes, int start, int len) {
328
328
* {@inheritDoc}
329
329
* <p>
330
330
* Source code: {@code _randbelow} method in the file
331
- * <a href="https://svn. python.org/projects/python/trunk /Lib/random.py">random.py</a>.
331
+ * <a href="https://github.com/ python/cpython/blob/master /Lib/random.py">random.py</a>.
332
332
*/
333
333
@ Override
334
334
public long nextLong (long n ) throws IllegalArgumentException {
@@ -350,7 +350,7 @@ public long nextLong(long n) throws IllegalArgumentException {
350
350
* {@inheritDoc}
351
351
* <p>
352
352
* Source code: {@code _randbelow} method in the file
353
- * <a href="https://svn. python.org/projects/python/trunk /Lib/random.py">random.py</a>.
353
+ * <a href="https://github.com/ python/cpython/blob/master /Lib/random.py">random.py</a>.
354
354
*/
355
355
@ Override
356
356
public long prevLong (long n ) throws IllegalArgumentException {
@@ -369,13 +369,4 @@ public long prevLong(long n) throws IllegalArgumentException {
369
369
throw new IllegalArgumentException ("n must be strictly positive" );
370
370
371
371
}
372
-
373
- private static int [] reverseArray (int [] seed ) {
374
- int [] seedReversed = new int [seed .length ];
375
- int j = seed .length ;
376
- for (int i = 0 ; i < seedReversed .length ; i ++) {
377
- seedReversed [i ] = seed [--j ];
378
- }
379
- return seedReversed ;
380
- }
381
372
}
0 commit comments