Search in sources :

Example 1 with MersenneRandom

use of org.terasology.engine.utilities.random.MersenneRandom in project Terasology by MovingBlocks.

the class MersenneRandomTest method testCorrectness.

/**
 * Compare results with http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out
 */
@Test
public void testCorrectness() {
    MersenneRandom r = new MersenneRandom(new int[] { 0x123, 0x234, 0x345, 0x456 });
    logger.debug("Compare MersenneTwisterFast with new (2002/1/26) seeding mechanism with original result data..");
    for (int j = 0; j < 1000; j++) {
        // first, convert the int from signed to "unsigned"
        long l = r.nextInt();
        if (l < 0) {
            // Integer.MAX_VALUE
            l += 4294967296L;
        }
        assertEquals(FIRST_1000_VALS[j], l);
    }
    logger.debug("Success");
}
Also used : MersenneRandom(org.terasology.engine.utilities.random.MersenneRandom) Test(org.junit.jupiter.api.Test)

Example 2 with MersenneRandom

use of org.terasology.engine.utilities.random.MersenneRandom in project Terasology by MovingBlocks.

the class MersenneRandomTest method testSpeed.

/**
 * Perform some speed tests and write results to logger
 */
@Test
public void testSpeed() {
    final long seed = 4357;
    final int warmUpCount = 10000;
    final int count = 10000000;
    int sum;
    long start;
    logger.info("Time to test grabbing {} ints", count);
    // -------------------------------------------------------
    java.util.Random rr = new java.util.Random(seed);
    // warmup
    sum = 0;
    for (int j = 0; j < warmUpCount; j++) {
        sum += rr.nextInt();
    }
    sum = 0;
    start = System.nanoTime();
    for (int j = 0; j < count; j++) {
        sum += rr.nextInt();
    }
    logger.info("java.util.Random: {}ms.", (System.nanoTime() - start) / 1000000);
    logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
    // -------------------------------------------------------
    FastRandom fr = new FastRandom(seed);
    // warmup
    sum = 0;
    for (int j = 0; j < warmUpCount; j++) {
        sum += fr.nextInt();
    }
    sum = 0;
    start = System.nanoTime();
    for (int j = 0; j < count; j++) {
        sum += fr.nextInt();
    }
    logger.info("FastRandom: {}ms.", (System.nanoTime() - start) / 1000000);
    logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
    // -------------------------------------------------------
    MersenneRandom r = new MersenneRandom(seed);
    // warmup
    sum = 0;
    for (int j = 0; j < warmUpCount; j++) {
        sum += r.nextInt();
    }
    sum = 0;
    start = System.nanoTime();
    for (int j = 0; j < count; j++) {
        sum += r.nextInt();
    }
    logger.info("MersenneRandom: {}ms.", (System.nanoTime() - start) / 1000000);
    logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
}
Also used : MersenneRandom(org.terasology.engine.utilities.random.MersenneRandom) MersenneRandom(org.terasology.engine.utilities.random.MersenneRandom) FastRandom(org.terasology.engine.utilities.random.FastRandom) FastRandom(org.terasology.engine.utilities.random.FastRandom) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 MersenneRandom (org.terasology.engine.utilities.random.MersenneRandom)2 FastRandom (org.terasology.engine.utilities.random.FastRandom)1