001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    
019    package org.activemq.transport.reliable;
020    
021    /**
022     * A Shuffled Multiple Combined Linear Congruential Generator Uses L'Ecuyer's CLCG4 with a Bays-Durham shuffle. From
023     * <cite>Numerical Recipes inC </cite> This produces a more random stream of results than just
024     * <code>java.util.Random</code>
025     * 
026     * @version $Revision: 1.1.1.1 $
027     */
028    public class SMLCGRandom {
029        private static final long MULTIPLIER_1 = 40014;
030        private static final long MOD_1 = 2147483563;
031        private static final long MULTIPLIER_2 = 40692;
032        private static final long MOD_2 = 2147483399;
033        private static final int SHUFFLE_LEN = 32;
034        private static final int WARMUP_LENGTH = 19;
035        private int generated_1, generated_2, state;
036        private int[] shuffle;
037    
038        /**
039         * Creates a new pseudorandom number generator, seeded from the current time.
040         */
041        public SMLCGRandom() {
042            this(System.currentTimeMillis());
043        }
044    
045        /**
046         * Creates the generator with the provided seed
047         * 
048         * @param seed
049         */
050        public SMLCGRandom(long seed) {
051            shuffle = new int[SHUFFLE_LEN];
052            this.setSeed(seed);
053        }
054    
055        /**
056         * Set the seed for the random generator
057         * @param seed
058         * @throws IllegalArgumentException
059         */
060        public void setSeed(long seed) throws IllegalArgumentException {
061            int i;
062            generated_1 = generated_2 = (int) (seed & 0x7FFFFFFFFL);  
063            for (i = 0;i < WARMUP_LENGTH;i++) {
064                generated_1 = (int) ((generated_1 * MULTIPLIER_1) % MOD_1);
065            }
066            for (i = 0;i < SHUFFLE_LEN;i++) {
067                generated_1 = (int) ((generated_1 * MULTIPLIER_1) % MOD_1);
068                shuffle[(SHUFFLE_LEN - 1) - i] = generated_1;
069            }
070            state = shuffle[0];
071        }
072    
073        /**
074         * @return the next random, uniformly distrubted, <tt>short</tt> value
075         */
076        public short nextShort() {
077            return (short) ((((short) nextByte()) << 8) | ((short) (nextByte() & 0xFF)));
078        }
079    
080        /**
081         * @return the next random, uniformly distrubted, <tt>int</tt> value
082         */
083        public int nextInt() {
084            return (int) ((((int) nextShort()) << 16) | (((int) nextShort()) & 0xFFFF));
085        }
086    
087        /**
088         * @return the next random, uniformly distrubted, <tt>long</tt> value
089         */
090        public long nextLong() {
091            return (long) ((((long) nextInt()) << 32) | (((long) nextInt()) & 0xFFFFFFFFl));
092        }
093    
094        /**
095         * @return the next random, uniformly distributed, <tt>float</tt> value, greater than or equal to 0 and less than
096         * 1.
097         */
098        public float nextFloat() {
099            return (float) ((nextInt() & 0x7FFFFFFF) / (0x7FFFFFFF * 1.0));
100        }
101    
102        /**
103         * @return the next random, uniformly distributed, <tt>double</tt> value, greater than or equal to 0 and less than
104         * 1.
105         */
106        public double nextDouble() {
107            return (double) ((nextLong() & 0x7FFFFFFFFFFFFFFFl) / (0x7FFFFFFFFFFFFFFFl * 1.0));
108        }
109    
110        /**
111         * @return the next random, uniformly distrubted, <tt>byte</tt> value
112         */
113        public byte nextByte() {
114            int i = 0;
115            generated_1 = (int) ((generated_1 * MULTIPLIER_1) % MOD_1);
116            generated_2 = (int) ((generated_2 * MULTIPLIER_2) % MOD_2);
117            i = state / (1 + (((int) MOD_1) - 1) / SHUFFLE_LEN);
118            i = Math.abs(i);
119            state = (int) ((((long) shuffle[i]) + generated_2) % MOD_1);
120            shuffle[i] = generated_1;
121            return (byte) (state / (1 + (((int) MOD_1) - 1) / 256));
122        }
123    }