home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 1.4 KB | 59 lines |
-
- package simula.random;
-
- import java.util.Random;
-
- /**
- * This is the simplest implementation of the abstract class RandomEngine:
- * it is based on the existing pseudo-random java engine and is the basis
- * of the other generators in the hierarchy. Note however that
- * this generator and the java one draw different numbers on the same seed.
- */
-
- public class RandomEngineJava extends RandomEngine {
-
- private Random r;
- private int ibytes = 0;
-
- private int idat;
-
-
- // Costruttori
-
- /**
- * Creates a new random number generator. Its seed is initialized to a
- * value based on the current time.
- */
- public RandomEngineJava() { r = new Random(); }
- /**
- * Creates a new random number generator using the given seed.
- * @param seed The initial seed.
- */
- public RandomEngineJava(long seed) { r = new Random(seed); }
- /**
- * Returns the next pseudo-random byte from this generator.
- * @return the next pseudo-random byte from this generator.
- */
- public byte nextByte() {
- // Takes an int from the engine and returns it one byte a time.
- byte d;
-
- if (ibytes <= 0) {
- idat = r.nextInt();
- ibytes = 4;
- }
- d = (byte) idat;
- idat >>= 8;
- ibytes--;
- return d;
- }
- /**
- * Sets a new seed.
- * @param seed New initial seed.
- */
- public void setSeed(long seed) {
- super.setSeed(); // Notify change to superclass
- r.setSeed(seed);
- ibytes = 0; // Clears the buffer
- }
- }