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

📄 mersennetwisterfast.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                b ^= (b >>> 18);                        // TEMPERING_SHIFT_L(b)                                /* derived from nextDouble documentation in jdk 1.2 docs, see top */                v1 = 2 *                    (((((long)(y >>> 6)) << 27) + (z >>> 5)) / (double)(1L << 53))                    - 1;                v2 = 2 * (((((long)(a >>> 6)) << 27) + (b >>> 5)) / (double)(1L << 53))                    - 1;                s = v1 * v1 + v2 * v2;                } while (s >= 1);            double multiplier = Math.sqrt(-2 * Math.log(s)/s);            nextNextGaussian = v2 * multiplier;            haveNextNextGaussian = true;            return v1 * multiplier;            }        }                    public final float nextFloat()        {        int y;                if (mti >= N)   // generate N words at one time            {            int kk;                        for (kk = 0; kk < N - M; kk++)                {                y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1];                }            for (; kk < N-1; kk++)                {                y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1];                }            y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);            mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1];            mti = 0;            }          y = mt[mti++];        y ^= y >>> 11;                          // TEMPERING_SHIFT_U(y)        y ^= (y << 7) & TEMPERING_MASK_B;       // TEMPERING_SHIFT_S(y)        y ^= (y << 15) & TEMPERING_MASK_C;      // TEMPERING_SHIFT_T(y)        y ^= (y >>> 18);                        // TEMPERING_SHIFT_L(y)        return (y >>> 8) / ((float)(1 << 24));        }    /** Returns an integer drawn uniformly from 0 to n-1.  Suffice it to say,        n must be > 0, or an IllegalArgumentException is raised. */    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            {            int y;                    if (mti >= N)   // generate N words at one time                {                int kk;                                for (kk = 0; kk < N - M; kk++)                    {                    y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                    mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1];                    }                for (; kk < N-1; kk++)                    {                    y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                    mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1];                    }                y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);                mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1];                                mti = 0;                }                        y = mt[mti++];            y ^= y >>> 11;                          // TEMPERING_SHIFT_U(y)            y ^= (y << 7) & TEMPERING_MASK_B;       // TEMPERING_SHIFT_S(y)            y ^= (y << 15) & TEMPERING_MASK_C;      // TEMPERING_SHIFT_T(y)            y ^= (y >>> 18);                        // TEMPERING_SHIFT_L(y)                        return (int)((n * (long) (y >>> 1) ) >> 31);            }                int bits, val;        do             {            int y;                        if (mti >= N)   // generate N words at one time                {                int kk;                                for (kk = 0; kk < N - M; kk++)                    {                    y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                    mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1];                    }                for (; kk < N-1; kk++)                    {                    y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);                    mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1];                    }                y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);                mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1];                                mti = 0;                }                        y = mt[mti++];            y ^= y >>> 11;                          // TEMPERING_SHIFT_U(y)            y ^= (y << 7) & TEMPERING_MASK_B;       // TEMPERING_SHIFT_S(y)            y ^= (y << 15) & TEMPERING_MASK_C;      // TEMPERING_SHIFT_T(y)            y ^= (y >>> 18);                        // TEMPERING_SHIFT_L(y)                    bits = (y >>> 1);            val = bits % n;            } while(bits - val + (n-1) < 0);        return val;        }        /**     * Tests the code.     */    public static void main(String args[])        {         int j;        MersenneTwisterFast 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 MersenneTwisterFast(1);        r.nextGaussian();  // loads the later gaussian into the state        System.out.println("1:  " + r.nextGaussian());        r = new MersenneTwisterFast(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 MersenneTwisterFast(4357);        System.out.println("Output of MersenneTwisterFast.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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 MersenneTwisterFast();        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 + -