home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 1.4 KB | 62 lines |
-
- package simula.random;
-
- /**
- ** Implements a multipling congruential generator "Minimal Standard"
- ** by Park e Miller.
- ** The generation algorythm is the following:
- ** <P>
- ** <center>
- ** <em>I<sub>j+1</sub></em> = (<em>I<sub>j</sub></em> ╫ 16807) & 0x7FFFFFFF
- ** </center>
- */
-
- public class RandomEngineMCG extends RandomEngine {
- private long state;
-
-
- // Constructors
-
- /**
- ** Creates a new engine using current time as the seed
- */
-
- public RandomEngineMCG() {
- this.setSeed(System.currentTimeMillis());
- }
- /**
- ** Creates a new engine using given seed
- ** @param seed generator seed (shouldn't be NULL)
- */
- public RandomEngineMCG(long seed) throws IllegalArgumentException {
- this.setSeed(seed);
- }
- /**
- ** Return the next byte from engine
- ** @return next byte from this engine
- */
-
- public byte nextByte() {
- state = (state * 16807) & 0x7FFFFFFFL;
- return (byte) ((state >> 11) & 0xFF);
- }
- // Seed set up
-
- /**
- ** Resets the seed for the engine
- ** @param seed new seed for the engine
- */
- public void setSeed(long seed) throws IllegalArgumentException {
- int i;
-
- if (seed == 0) {
- throw new IllegalArgumentException("Il seme deve essere non nullo");
- }
- super.setSeed(); // Notifies this change to parent class
- state = seed & 0xFFFFFFFFL;
- // System warmup
- for (i = 0; i < 11; i++) {
- nextByte();
- }
- }
- }