001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2008  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix.tools;
021    
022    /**
023     * Random number generator used by Tetrinet (ie. the Random() function in Delphi)
024     *
025     * @author Emmanuel Bourg
026     * @version $Revision: 743 $, $Date: 2008-08-27 11:22:05 +0200 (Wed, 27 Aug 2008) $
027     */
028    public class TetrinetRandomGenerator
029    {
030        private static final int INCREMENT = 1;
031        private static final int MULTIPLIER = 0x08088405;
032        private static final long MODULUS = 0x100000000L;
033    
034        private long value;
035    
036        public TetrinetRandomGenerator(int seed)
037        {
038            this.value = seed;
039        }
040    
041        public long getValue()
042        {
043            return value;
044        }
045    
046        public synchronized int nextInt(int L) {
047            nextValue();
048    
049            return (int) (value * L >> 32);
050        }
051    
052        void nextValue() {
053            value = (MULTIPLIER * value + INCREMENT) % MODULUS;
054        }
055    }