|
33 | 33 | import java.io.DataOutputStream; |
34 | 34 | import java.io.IOException; |
35 | 35 | import java.io.Serializable; |
36 | | -import java.util.Random; |
37 | 36 |
|
38 | 37 | /** |
39 | | - * <h2>MersenneTwister and MersenneTwisterFast</h2> |
| 38 | + * A faster version of {@link java.util.Random} functionality, |
| 39 | + * built on the Mersenne Twister algorithm. |
| 40 | + * |
| 41 | + * <h3>MersenneTwister and MersenneTwisterFast</h3> |
40 | 42 | * <p> |
41 | 43 | * <b>Version 17</b>, based on version MT199937(99/10/29) of the Mersenne |
42 | 44 | * Twister algorithm found at |
|
148 | 150 | * MersenneTwister can be used reliably on JDK version 1.1.5 or above. Earlier |
149 | 151 | * Java versions have serious bugs in java.util.Random; only MersenneTwisterFast |
150 | 152 | * (and not MersenneTwister nor java.util.Random) should be used with them. |
151 | | - * <h3>License</h3> Copyright (c) 2003 by Sean Luke. <br> |
| 153 | + * |
| 154 | + * <h3>License</h3> |
| 155 | + * Copyright (c) 2003 by Sean Luke. <br> |
152 | 156 | * Portions copyright (c) 1993 by Michael Lecuyer. <br> |
153 | 157 | * All rights reserved. <br> |
154 | 158 | * <p> |
@@ -191,10 +195,7 @@ public strictfp class MersenneTwisterFast implements Serializable, Cloneable { |
191 | 195 | // -- Sean |
192 | 196 |
|
193 | 197 | // Serialization |
194 | | - private static final long serialVersionUID = -8219700664442619525L; // locked |
195 | | - // as of |
196 | | - // Version |
197 | | - // 15 |
| 198 | + private static final long serialVersionUID = -8219700664442619525L; // locked as of Version 15 |
198 | 199 |
|
199 | 200 | // Period parameters |
200 | 201 | private static final int N = 624; |
@@ -1205,176 +1206,4 @@ public final int nextInt(final int n) { |
1205 | 1206 | while (bits - val + (n - 1) < 0); |
1206 | 1207 | return val; |
1207 | 1208 | } |
1208 | | - |
1209 | | - /** |
1210 | | - * Tests the code. |
1211 | | - */ |
1212 | | - public static void main(String args[]) { |
1213 | | - int j; |
1214 | | - |
1215 | | - MersenneTwisterFast r; |
1216 | | - |
1217 | | - // CORRECTNESS TEST |
1218 | | - // COMPARE WITH |
1219 | | - // http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out |
1220 | | - |
1221 | | - r = new MersenneTwisterFast(new int[] { 0x123, 0x234, 0x345, 0x456 }); |
1222 | | - System.out.println( |
1223 | | - "Output of MersenneTwisterFast with new (2002/1/26) seeding mechanism"); |
1224 | | - for (j = 0; j < 1000; j++) { |
1225 | | - // first, convert the int from signed to "unsigned" |
1226 | | - long l = (long) r.nextInt(); |
1227 | | - if (l < 0) l += 4294967296L; // max int value |
1228 | | - String s = String.valueOf(l); |
1229 | | - while (s.length() < 10) |
1230 | | - s = " " + s; // buffer |
1231 | | - System.out.print(s + " "); |
1232 | | - if (j % 5 == 4) System.out.println(); |
1233 | | - } |
1234 | | - |
1235 | | - // SPEED TEST |
1236 | | - |
1237 | | - final long SEED = 4357; |
1238 | | - |
1239 | | - int xx; |
1240 | | - long ms; |
1241 | | - System.out.println("\nTime to test grabbing 100000000 ints"); |
1242 | | - |
1243 | | - Random rr = new Random(SEED); |
1244 | | - xx = 0; |
1245 | | - ms = System.currentTimeMillis(); |
1246 | | - for (j = 0; j < 100000000; j++) |
1247 | | - xx += rr.nextInt(); |
1248 | | - System.out.println("java.util.Random: " + (System.currentTimeMillis() - |
1249 | | - ms) + " Ignore this: " + xx); |
1250 | | - |
1251 | | - r = new MersenneTwisterFast(SEED); |
1252 | | - ms = System.currentTimeMillis(); |
1253 | | - xx = 0; |
1254 | | - for (j = 0; j < 100000000; j++) |
1255 | | - xx += r.nextInt(); |
1256 | | - System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - |
1257 | | - ms) + " Ignore this: " + xx); |
1258 | | - |
1259 | | - // TEST TO COMPARE TYPE CONVERSION BETWEEN |
1260 | | - // MersenneTwisterFast.java AND MersenneTwister.java |
1261 | | - |
1262 | | - System.out.println("\nGrab the first 1000 booleans"); |
1263 | | - r = new MersenneTwisterFast(SEED); |
1264 | | - for (j = 0; j < 1000; j++) { |
1265 | | - System.out.print(r.nextBoolean() + " "); |
1266 | | - if (j % 8 == 7) System.out.println(); |
1267 | | - } |
1268 | | - if (!(j % 8 == 7)) System.out.println(); |
1269 | | - |
1270 | | - System.out.println( |
1271 | | - "\nGrab 1000 booleans of increasing probability using nextBoolean(double)"); |
1272 | | - r = new MersenneTwisterFast(SEED); |
1273 | | - for (j = 0; j < 1000; j++) { |
1274 | | - System.out.print(r.nextBoolean((double) (j / 999.0)) + " "); |
1275 | | - if (j % 8 == 7) System.out.println(); |
1276 | | - } |
1277 | | - if (!(j % 8 == 7)) System.out.println(); |
1278 | | - |
1279 | | - System.out.println( |
1280 | | - "\nGrab 1000 booleans of increasing probability using nextBoolean(float)"); |
1281 | | - r = new MersenneTwisterFast(SEED); |
1282 | | - for (j = 0; j < 1000; j++) { |
1283 | | - System.out.print(r.nextBoolean((float) (j / 999.0f)) + " "); |
1284 | | - if (j % 8 == 7) System.out.println(); |
1285 | | - } |
1286 | | - if (!(j % 8 == 7)) System.out.println(); |
1287 | | - |
1288 | | - byte[] bytes = new byte[1000]; |
1289 | | - System.out.println("\nGrab the first 1000 bytes using nextBytes"); |
1290 | | - r = new MersenneTwisterFast(SEED); |
1291 | | - r.nextBytes(bytes); |
1292 | | - for (j = 0; j < 1000; j++) { |
1293 | | - System.out.print(bytes[j] + " "); |
1294 | | - if (j % 16 == 15) System.out.println(); |
1295 | | - } |
1296 | | - if (!(j % 16 == 15)) System.out.println(); |
1297 | | - |
1298 | | - byte b; |
1299 | | - System.out.println( |
1300 | | - "\nGrab the first 1000 bytes -- must be same as nextBytes"); |
1301 | | - r = new MersenneTwisterFast(SEED); |
1302 | | - for (j = 0; j < 1000; j++) { |
1303 | | - System.out.print((b = r.nextByte()) + " "); |
1304 | | - if (b != bytes[j]) System.out.print("BAD "); |
1305 | | - if (j % 16 == 15) System.out.println(); |
1306 | | - } |
1307 | | - if (!(j % 16 == 15)) System.out.println(); |
1308 | | - |
1309 | | - System.out.println("\nGrab the first 1000 shorts"); |
1310 | | - r = new MersenneTwisterFast(SEED); |
1311 | | - for (j = 0; j < 1000; j++) { |
1312 | | - System.out.print(r.nextShort() + " "); |
1313 | | - if (j % 8 == 7) System.out.println(); |
1314 | | - } |
1315 | | - if (!(j % 8 == 7)) System.out.println(); |
1316 | | - |
1317 | | - System.out.println("\nGrab the first 1000 ints"); |
1318 | | - r = new MersenneTwisterFast(SEED); |
1319 | | - for (j = 0; j < 1000; j++) { |
1320 | | - System.out.print(r.nextInt() + " "); |
1321 | | - if (j % 4 == 3) System.out.println(); |
1322 | | - } |
1323 | | - if (!(j % 4 == 3)) System.out.println(); |
1324 | | - |
1325 | | - System.out.println("\nGrab the first 1000 ints of different sizes"); |
1326 | | - r = new MersenneTwisterFast(SEED); |
1327 | | - int max = 1; |
1328 | | - for (j = 0; j < 1000; j++) { |
1329 | | - System.out.print(r.nextInt(max) + " "); |
1330 | | - max *= 2; |
1331 | | - if (max <= 0) max = 1; |
1332 | | - if (j % 4 == 3) System.out.println(); |
1333 | | - } |
1334 | | - if (!(j % 4 == 3)) System.out.println(); |
1335 | | - |
1336 | | - System.out.println("\nGrab the first 1000 longs"); |
1337 | | - r = new MersenneTwisterFast(SEED); |
1338 | | - for (j = 0; j < 1000; j++) { |
1339 | | - System.out.print(r.nextLong() + " "); |
1340 | | - if (j % 3 == 2) System.out.println(); |
1341 | | - } |
1342 | | - if (!(j % 3 == 2)) System.out.println(); |
1343 | | - |
1344 | | - System.out.println("\nGrab the first 1000 longs of different sizes"); |
1345 | | - r = new MersenneTwisterFast(SEED); |
1346 | | - long max2 = 1; |
1347 | | - for (j = 0; j < 1000; j++) { |
1348 | | - System.out.print(r.nextLong(max2) + " "); |
1349 | | - max2 *= 2; |
1350 | | - if (max2 <= 0) max2 = 1; |
1351 | | - if (j % 4 == 3) System.out.println(); |
1352 | | - } |
1353 | | - if (!(j % 4 == 3)) System.out.println(); |
1354 | | - |
1355 | | - System.out.println("\nGrab the first 1000 floats"); |
1356 | | - r = new MersenneTwisterFast(SEED); |
1357 | | - for (j = 0; j < 1000; j++) { |
1358 | | - System.out.print(r.nextFloat() + " "); |
1359 | | - if (j % 4 == 3) System.out.println(); |
1360 | | - } |
1361 | | - if (!(j % 4 == 3)) System.out.println(); |
1362 | | - |
1363 | | - System.out.println("\nGrab the first 1000 doubles"); |
1364 | | - r = new MersenneTwisterFast(SEED); |
1365 | | - for (j = 0; j < 1000; j++) { |
1366 | | - System.out.print(r.nextDouble() + " "); |
1367 | | - if (j % 3 == 2) System.out.println(); |
1368 | | - } |
1369 | | - if (!(j % 3 == 2)) System.out.println(); |
1370 | | - |
1371 | | - System.out.println("\nGrab the first 1000 gaussian doubles"); |
1372 | | - r = new MersenneTwisterFast(SEED); |
1373 | | - for (j = 0; j < 1000; j++) { |
1374 | | - System.out.print(r.nextGaussian() + " "); |
1375 | | - if (j % 3 == 2) System.out.println(); |
1376 | | - } |
1377 | | - if (!(j % 3 == 2)) System.out.println(); |
1378 | | - |
1379 | | - } |
1380 | 1209 | } |
0 commit comments