package net.nanopool;

public final class CheapRandom {
    private int prevRandom;
    
    public CheapRandom() {
        this.prevRandom = (int) System.nanoTime();
    }
    
    public int nextInt() {
        return prevRandom = xorShift(prevRandom
                ^ (int) Thread.currentThread().getId());
    }
    
    public int nextAbs(int min, int max) {
        assert min < max;
        final int x = nextInt();
        final int mask = x >> 32 * 1073741824 - 1; // Twiddle-twiddle,
        return ((x + mask) ^ mask) % (max - min) + min; // magic-fiddle! :)
    }
    
    public int xorShift(int seed) {
        /*
         * XorShift, George Marsaglia. http://www.jstatsoft.org/v08/i14
         */
        seed ^= (seed << 6);
        seed ^= (seed >>> 21);
        return seed ^ (seed << 7);
    }
    
    public static void main(String[] args) {
        runTest();
        runTest();
        runTest();
        runTest();
        runTest();
        runTest();
        runTest();
    }
    
    private static void runTest() {
        long start = System.nanoTime();
        CheapRandom rand = new CheapRandom();
        // java.util.Random rand = new java.util.Random();
        int r = 0;
        for (int i = 0; i < 1000000; i++) {
            r = rand.nextInt();
        }
        long end = System.nanoTime();
        long elapsed = end - start;
        System.out.printf("Time used: %s ms. r = %s\n", elapsed / 1000000.0, r);
    }
}

@@ -810,647 +810,6 @@
   %7D%0A
-    %0A    public static void main(String%5B%5D args) %7B%0A        runTest();%0A        runTest();%0A        runTest();%0A        runTest();%0A        runTest();%0A        runTest();%0A        runTest();%0A    %7D%0A    %0A    private static void runTest() %7B%0A        long start = System.nanoTime();%0A        CheapRandom rand = new CheapRandom();%0A        // java.util.Random rand = new java.util.Random();%0A        int r = 0;%0A        for (int i = 0; i %3C 1000000; i++) %7B%0A            r = rand.nextInt();%0A        %7D%0A        long end = System.nanoTime();%0A        long elapsed = end - start;%0A        System.out.printf(%22Time used: %25s ms. r = %25s%5Cn%22, elapsed / 1000000.0, r);%0A    %7D%0A
 %7D%0A

remove test/bench cruft from CheapRandom