home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 1.3 KB | 57 lines |
-
- package simula.random;
-
- /**
- ** Implements a Linear Congruential Generator (LCG)
- ** This is a toy generator and we don't recommend it for serious uses
- ** This is based on the following:
- ** <p>
- ** <center>
- ** <em>I<sub>j+1</sub></em> = (<em>I<sub>j</sub></em> ╫ 1103515245 + 12345) & 0x7FFFFFFF
- ** </center>
- ** <p>
- */
-
- public class RandomEngineLCG extends RandomEngine {
- /**
- ** Generator state in bytes generations
- */
- private long state;
-
-
- // Constructors
-
- /**
- ** Creates a new engine with current time used as the seed
- */
- public RandomEngineLCG() {
- this.setSeed(System.currentTimeMillis());
- }
- /**
- ** Creates an new engine with given seed
- ** @param seed generator seed
- */
- public RandomEngineLCG(long seed) {
- this.setSeed(seed);
- }
- /**
- ** Returns the next pseudoRandom byte
- ** @return the next byte from the generator.
- */
- public byte nextByte() {
- // Higher bits are better
- // No buuffering in this engine. Everytime all bits are generated
- state = (state * 1103515245L + 12345L) & 0x7FFFFFFFL;
- return (byte) ((state >> 11) & 0xFF);
- }
- // Seeds check
-
- /**
- ** Resets the generator seed
- ** @param seed generator seed
- */
- public void setSeed(long seed) {
- super.setSeed(); // notifies to parent class this change
- state = seed & 0xFFFFFFFFL;
- }
- }