Skip to content

Commit 465974e

Browse files
committed
v0.3 - simplified seed initialization for mersenne twister
1 parent 9a1e46b commit 465974e

11 files changed

+394
-116
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ I got the idea of reversing the generators from several places, after I had to b
3131
<dependency>
3232
<groupId>ro.derbederos</groupId>
3333
<artifactId>untwist</artifactId>
34-
<version>0.2</version>
34+
<version>0.3</version>
3535
</dependency>
3636
```
3737
#### Gradle dependency
3838
```groovy
39-
compile 'ro.derbederos:untwist:0.2'
39+
compile 'ro.derbederos:untwist:0.3'
4040
```
4141

4242
Enjoy using them!

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>ro.derbederos</groupId>
2424
<artifactId>untwist</artifactId>
25-
<version>0.2</version>
25+
<version>0.3</version>
2626

2727
<name>untwist</name>
2828
<description>Java 8+ collection of PRNG's from CLR (.NET), FreePascal, TurboPascal, Python.</description>

src/main/java/ro/derbederos/untwist/ArrayUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ public static int[] getTree(int n, RandomGenerator randomizer) {
5656
}
5757

5858
public static int[] getPermutation(int n, RandomGenerator randomizer) {
59-
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
6059
return getPermutation(n, 1, randomizer);
6160
}
6261

6362
public static int[] getPermutationInsideOut(int n, RandomGenerator randomizer) {
64-
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
63+
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_.22inside-out.22_algorithm
6564
int[] perm = new int[n];
6665
for (int i = 1; i < n; i++) {
6766
int j = randomizer.nextInt(i + 1);

src/main/java/ro/derbederos/untwist/FreePascalRandom.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ public FreePascalRandom(long seed) {
7676
super(seed);
7777
}
7878

79-
/**
80-
* {@inheritDoc}
81-
*/
82-
@Override
83-
public void setSeed(int seed) {
84-
super.setSeed(seed);
85-
}
86-
8779
/**
8880
* {@inheritDoc}
8981
* <p>
@@ -96,7 +88,7 @@ public void setSeed(long seed) {
9688
if (high == 0) {
9789
setSeed((int) seed);
9890
} else {
99-
setSeed(new int[]{high, (int) (seed & 0xFFFFFFFFL)});
91+
setSeed(new int[]{(int) (seed & 0xFFFFFFFFL), high});
10092
}
10193
}
10294

@@ -110,14 +102,18 @@ public void setSeed(int[] seed) {
110102
if (seed.length == 1) {
111103
setSeed(seed[0]);
112104
} else {
113-
super.setSeed(reverseArray(seed));
105+
super.setSeed(seed);
114106
}
115107
}
116108

117109
/**
118110
* {@inheritDoc}
119111
* <p>
120112
* It uses only 32 bits to generate a double precision float number.
113+
* <p>
114+
* This method is the same as {@code genrand_real2} method in the
115+
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c">original implementation</a>
116+
* of theMersenne Twister.
121117
*/
122118
@Override
123119
public double nextDouble() {
@@ -138,6 +134,10 @@ public double prevDouble() {
138134
* {@inheritDoc}
139135
* <p>
140136
* It simply calls {@link #nextDouble()} and does a cast.
137+
* <p>
138+
* This method is the same as {@code genrand_real2} method in the
139+
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c">original implementation</a>
140+
* of theMersenne Twister.
141141
*/
142142
@Override
143143
public float nextFloat() {
@@ -238,13 +238,4 @@ public long prevLong(long n) throws IllegalArgumentException {
238238
return 0;
239239
}
240240
}
241-
242-
private static int[] reverseArray(int[] seed) {
243-
int[] seedReversed = new int[seed.length];
244-
int j = seed.length;
245-
for (int i = 0; i < seedReversed.length; i++) {
246-
seedReversed[i] = seed[--j];
247-
}
248-
return seedReversed;
249-
}
250241
}

src/main/java/ro/derbederos/untwist/MersenneTwisterPy3k.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
* A Mersenne Twister subclass which generates the same numbers as the Python 3 implementation.
2323
* Source code uses as reference is part of the files:
2424
* <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>
2728
* </ul>
29+
* Test
2830
*/
2931
public class MersenneTwisterPy3k extends ReversibleMersenneTwister {
3032

@@ -92,28 +94,19 @@ public void setSeed(long seed) {
9294
if (high == 0) {
9395
setSeed(new int[]{(int) seed});
9496
} else {
95-
setSeed(new int[]{high, (int) (seed & 0xFFFFFFFFL)});
97+
setSeed(new int[]{(int) (seed & 0xFFFFFFFFL), high});
9698
}
9799
}
98100

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-
112101
/**
113102
* {@inheritDoc}
114103
* <p>
115104
* 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.
117110
*/
118111
@Override
119112
public double nextDouble() {
@@ -124,7 +117,7 @@ public double nextDouble() {
124117
* {@inheritDoc}
125118
* <p>
126119
* 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>.
128121
*/
129122
@Override
130123
public double prevDouble() {
@@ -135,6 +128,13 @@ public double prevDouble() {
135128
* {@inheritDoc}
136129
* <p>
137130
* 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.
138138
*/
139139
@Override
140140
public float nextFloat() {
@@ -155,7 +155,7 @@ public float prevFloat() {
155155
* {@inheritDoc}
156156
* <p>
157157
* 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>.
159159
*/
160160
@Override
161161
public int nextInt(int n) throws IllegalArgumentException {
@@ -174,7 +174,7 @@ public int nextInt(int n) throws IllegalArgumentException {
174174
* {@inheritDoc}
175175
* <p>
176176
* 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>.
178178
*/
179179
@Override
180180
public int prevInt(int n) throws IllegalArgumentException {
@@ -196,7 +196,7 @@ public int prevInt(int n) throws IllegalArgumentException {
196196
* when called for 8 bytes.
197197
* <p>
198198
* 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>.
200200
*/
201201
@Override
202202
public long nextLong() {
@@ -212,7 +212,7 @@ public long nextLong() {
212212
* when called for 8 bytes.
213213
* <p>
214214
* 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>.
216216
*/
217217
@Override
218218
public long prevLong() {
@@ -225,7 +225,7 @@ public long prevLong() {
225225
* {@inheritDoc}
226226
* <p>
227227
* 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>.
229229
*/
230230
@Override
231231
public void nextBytes(byte[] bytes, int start, int len) {
@@ -243,7 +243,7 @@ public void nextBytes(byte[] bytes, int start, int len) {
243243
* {@inheritDoc}
244244
* <p>
245245
* 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>.
247247
*/
248248
@Override
249249
public void nextBytes(byte[] bytes) {
@@ -275,7 +275,7 @@ private void nextBytesFill(byte[] bytes, int start, int len) {
275275
* {@inheritDoc}
276276
* <p>
277277
* 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>.
279279
*/
280280
@Override
281281
public void prevBytes(byte[] bytes) {
@@ -286,7 +286,7 @@ public void prevBytes(byte[] bytes) {
286286
* {@inheritDoc}
287287
* <p>
288288
* 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>.
290290
*/
291291
@Override
292292
public void prevBytes(byte[] bytes, int start, int len) {
@@ -328,7 +328,7 @@ private void prevBytesFill(byte[] bytes, int start, int len) {
328328
* {@inheritDoc}
329329
* <p>
330330
* 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>.
332332
*/
333333
@Override
334334
public long nextLong(long n) throws IllegalArgumentException {
@@ -350,7 +350,7 @@ public long nextLong(long n) throws IllegalArgumentException {
350350
* {@inheritDoc}
351351
* <p>
352352
* 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>.
354354
*/
355355
@Override
356356
public long prevLong(long n) throws IllegalArgumentException {
@@ -369,13 +369,4 @@ public long prevLong(long n) throws IllegalArgumentException {
369369
throw new IllegalArgumentException("n must be strictly positive");
370370

371371
}
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-
}
381372
}

src/main/java/ro/derbederos/untwist/ReversibleMersenneTwister.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222

2323
/**
24+
* This class is a copy of {@link org.apache.commons.math3.random.MersenneTwister}
25+
* which also implements {@link ReverseRandomGenerator}.
26+
* <p>
2427
* This class implements a powerful pseudo-random number generator
2528
* developed by Makoto Matsumoto and Takuji Nishimura during
2629
* 1996-1997.

0 commit comments

Comments
 (0)