⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mersennetwister.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /** This method is missing from jdk 1.0.x and below.  JDK 1.1        includes this for us, but what the heck.*/    public boolean nextBoolean() {return next(1) != 0;}    /** This method is missing from JDK 1.1 and below.  JDK 1.2        includes this for us, but what the heck. */    public int nextInt(int n) {        if (n<=0)            throw new IllegalArgumentException("n must be positive");        if ((n & -n) == n)  // i.e., n is a power of 2            return (int)((n * (long)next(31)) >> 31);        int bits, val;        do {            bits = next(31);            val = bits % n;        } while(bits - val + (n-1) < 0);        return val;    }    /** A bug fix for versions of JDK 1.1 and below.  JDK 1.2 fixes        this for us, but what the heck. */    public double nextDouble()        {        return (((long)next(26) << 27) + next(27))            / (double)(1L << 53);        }    /** A bug fix for versions of JDK 1.1 and below.  JDK 1.2 fixes        this for us, but what the heck. */    public float nextFloat()        {        return next(24) / ((float)(1 << 24));        }    /** A bug fix for all versions of the JDK.  The JDK appears to        use all four bytes in an integer as independent byte values!        Totally wrong. I've submitted a bug report. */    public void nextBytes(byte[] bytes)            {        for (int x=0;x<bytes.length;x++)            bytes[x] = (byte)next(8);        }    /** For completeness' sake, though it's not in java.util.Random.  */        public char nextChar()        {        // chars are 16-bit UniCode values        return (char)(next(16));        }    /** For completeness' sake, though it's not in java.util.Random. */        public short nextShort()        {        return (short)(next(16));        }    /** For completeness' sake, though it's not in java.util.Random.  */    public byte nextByte()        {        return (byte)(next(8));        }    /**     * Tests the code.     */    public static void main(String args[])        {         int j;        MersenneTwister r;        // UNCOMMENT THIS TO TEST FOR PROPER GAUSSIAN STATE INITIALIZATION                /*        System.out.println("If the gaussian state is properly initialized when setSeed() is called,\nthen #1 != #2, but #1 == #3\nIt's known that java 1.0.2 doesn't do gaussian initialization right,\nso setSeed() may result in one last gaussian drawn from the *previous* seed.");        r = new MersenneTwister(1);        r.nextGaussian();  // loads the later gaussian into the state        System.out.println("1:  " + r.nextGaussian());        r = new MersenneTwister(1);        r.nextGaussian(); // loads the later gaussian into the state        r.setSeed(1);     // should reset the gaussian state        System.out.println("2:  " + r.nextGaussian());        System.out.println("3:  " + r.nextGaussian());        */                // UNCOMMENT THIS TO TEST FOR CORRECTNESS        // COMPARE WITH http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.out                /*        r = new MersenneTwister(4357);        System.out.println("Output of MersenneTwister.java");        for (j=0;j<1000;j++)            {            // first, convert the int from signed to "unsigned"            long l = (long)r.nextInt();            if (l < 0 ) l += 4294967296L;  // max int value            String s = String.valueOf(l);            while(s.length() < 10) s = " " + s;  // buffer            System.out.print(s + " ");            if (j%8==7) System.out.println();                   }        */        // UNCOMMENT THIS TO TEST FOR SPEED        /*        r = new MersenneTwister();        System.out.println("\nTime to test grabbing 10000000 ints");        long ms = System.currentTimeMillis();        int xx=0;        for (j = 0; j < 10000000; j++)            xx += r.nextInt();        System.out.println("Mersenne Twister: " + (System.currentTimeMillis()-ms + "          Ignore this: " + xx));        Random rr = new Random(1);        xx = 0;        ms = System.currentTimeMillis();        for (j = 0; j < 10000000; j++)            xx += rr.nextInt();        System.out.println("java.util.Random: " + (System.currentTimeMillis()-ms + "          Ignore this: " + xx));        */                // UNCOMMENT THIS TO DO TEST DIFFERENT TYPE OUTPUTS        // THIS CAN BE USED TO COMPARE THE DIFFERENCE BETWEEN        // MersenneTwisterFast.java AND MersenneTwister.java        /*        System.out.println("\nGrab the first 1000 booleans");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextBoolean() + " ");            if (j%8==7) System.out.println();            }        if (!(j%8==7)) System.out.println();        byte[] bytes = new byte[1000];        System.out.println("\nGrab the first 1000 bytes using nextBytes");        r = new MersenneTwister();        r.nextBytes(bytes);        for (j = 0; j < 1000; j++)            {            System.out.print(bytes[j] + " ");            if (j%16==15) System.out.println();            }        if (!(j%16==15)) System.out.println();                byte b;        System.out.println("\nGrab the first 1000 bytes -- must be same as nextBytes");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print((b = r.nextByte()) + " ");            if (b!=bytes[j]) System.out.print("BAD ");            if (j%16==15) System.out.println();            }        if (!(j%16==15)) System.out.println();        System.out.println("\nGrab the first 1000 shorts");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextShort() + " ");            if (j%8==7) System.out.println();            }        if (!(j%8==7)) System.out.println();        System.out.println("\nGrab the first 1000 ints");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextInt() + " ");            if (j%4==3) System.out.println();            }        if (!(j%4==3)) System.out.println();        System.out.println("\nGrab the first 1000 ints of different sizes");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextInt(j+1) + " ");            if (j%4==3) System.out.println();            }        if (!(j%4==3)) System.out.println();        System.out.println("\nGrab the first 1000 longs");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextLong() + " ");            if (j%3==2) System.out.println();            }        if (!(j%3==2)) System.out.println();        System.out.println("\nGrab the first 1000 floats");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextFloat() + " ");            if (j%4==3) System.out.println();            }        if (!(j%4==3)) System.out.println();        System.out.println("\nGrab the first 1000 doubles");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextDouble() + " ");            if (j%3==2) System.out.println();            }        if (!(j%3==2)) System.out.println();        System.out.println("\nGrab the first 1000 gaussian doubles");        r = new MersenneTwister();        for (j = 0; j < 1000; j++)            {            System.out.print(r.nextGaussian() + " ");            if (j%3==2) System.out.println();            }        if (!(j%3==2)) System.out.println();        */        }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -