home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_rand / min_stand.e < prev    next >
Text File  |  1999-06-05  |  2KB  |  90 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. class MIN_STAND
  13.    --
  14.    -- Implements the Minimal Standard generator from Press et. al.
  15.    -- Numerical Recipies.
  16.    --
  17.  
  18. inherit GEN_RAND;
  19.  
  20. creation make, with_seed
  21.  
  22. feature
  23.  
  24.    im: INTEGER is 2147483647;
  25.  
  26. feature {NONE}
  27.  
  28.    ia: INTEGER is 16807;
  29.  
  30.    iq: INTEGER is 127773;
  31.  
  32.    ir: INTEGER is 2836;
  33.  
  34.    seed:INTEGER
  35.  
  36. feature {NONE}
  37.  
  38.    make is
  39.       local
  40.          seed_init: INTEGER;
  41.       do
  42.          seed_init := 1 + Current.to_pointer.hash_code;
  43.          from
  44.          until
  45.             seed_init < im
  46.          loop
  47.             seed_init := seed_init - iq;
  48.          end;
  49.          with_seed(seed_init);
  50.       end;
  51.  
  52.    with_seed(seed_value: INTEGER) is
  53.       require
  54.          valid_seed: seed_value > 0 and seed_value < im
  55.       do
  56.          seed:= seed_value;
  57.          next;
  58.       end;
  59.  
  60. feature
  61.  
  62.    next is
  63.       local
  64.          k: INTEGER;
  65.       do
  66.          k := seed // iq;
  67.          seed := ia * (seed -k * iq) - ir * k;
  68.          if seed < 0 then
  69.             seed := seed + im;
  70.          end;
  71.       end;
  72.  
  73.    last_real: REAL is
  74.       do
  75.          Result := (seed/im).to_real;
  76.       end;
  77.  
  78.    last_integer(n:INTEGER): INTEGER is
  79.       do
  80.          Result := seed \\ n+1;
  81.       end;
  82.  
  83. invariant
  84.  
  85.    good_seed: seed > 0 and seed < im
  86.  
  87. end -- MIN_STAND
  88.  
  89.  
  90.