home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / random / RandomEngineMCG.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  1.4 KB  |  62 lines

  1.  
  2. package simula.random;
  3.  
  4. /**
  5. ** Implements a multipling congruential generator "Minimal Standard" 
  6. ** by Park e Miller. 
  7. ** The generation algorythm is the following:
  8. ** <P>
  9. **    <center>
  10. **    <em>I<sub>j+1</sub></em> = (<em>I<sub>j</sub></em> ╫ 16807) & 0x7FFFFFFF
  11. **    </center>
  12. */
  13.  
  14. public class RandomEngineMCG extends RandomEngine {
  15.     private long state;
  16.  
  17.  
  18.     //  Constructors
  19.  
  20. /**
  21. ** Creates a new engine using current time as the seed
  22. */
  23.  
  24.     public RandomEngineMCG() {
  25.         this.setSeed(System.currentTimeMillis());
  26.     }
  27. /**
  28. ** Creates a new engine using given seed
  29. ** @param seed generator seed (shouldn't be NULL)
  30. */
  31.     public RandomEngineMCG(long seed) throws IllegalArgumentException {
  32.         this.setSeed(seed);
  33.     }
  34. /** 
  35. ** Return the next byte from engine
  36. ** @return next byte from this engine
  37. */
  38.  
  39.     public byte nextByte() {
  40.         state = (state * 16807) & 0x7FFFFFFFL;
  41.         return (byte) ((state >> 11) & 0xFF);
  42.     }
  43. //  Seed set up
  44.  
  45. /**
  46. ** Resets the seed for the engine
  47. ** @param seed new seed for the engine 
  48. */
  49.     public void setSeed(long seed) throws IllegalArgumentException {
  50.         int i;
  51.  
  52.         if (seed == 0) {
  53.             throw new IllegalArgumentException("Il seme deve essere non nullo");
  54.         }
  55.         super.setSeed();              // Notifies this change to parent class
  56.         state = seed & 0xFFFFFFFFL;
  57.         // System warmup 
  58.         for (i = 0; i < 11; i++) {
  59.             nextByte();
  60.         }
  61.     }
  62. }