home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / djgpp / include / randomin.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-20  |  4.1 KB  |  169 lines

  1. /* This is file randomin.h */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. // This may look like C code, but it is really -*- C++ -*-
  8. /* 
  9. Copyright (C) 1990 Free Software Foundation
  10.     adapted from a submission from John Reidl <riedl@cs.purdue.edu>
  11.  
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY.  No author or distributor
  15. accepts responsibility to anyone for the consequences of using it
  16. or for whether it serves any particular purpose or works at all,
  17. unless he says so in writing.  Refer to the GNU CC General Public
  18. License for full details.
  19.  
  20. Everyone is granted permission to copy, modify and redistribute
  21. GNU CC, but only under the conditions described in the
  22. GNU CC General Public License.   A copy of this license is
  23. supposed to have been given to you along with GNU CC so you
  24. can know your rights and responsibilities.  It should be in a
  25. file named COPYING.  Among other things, the copyright notice
  26. and this notice must be preserved on all copies.  
  27. */
  28.  
  29. #ifndef _RandomInteger_h
  30. #pragma once
  31. #define _RandomInteger_h 1
  32.  
  33. // RandomInteger uses a random number generator to generate an integer
  34. // in a specified range.  By default the range is 0..1.  Since in my
  35. // experience random numbers are often needed for a wide variety of
  36. // ranges in the same program, this generator accepts a new low or high value
  37. // as an argument to the asLong and operator() methods to temporarily 
  38. // override stored values
  39.  
  40. #include <math.h>
  41. #include "RNG.h"
  42.  
  43. class RandomInteger 
  44. {
  45.  protected:
  46.   RNG *pGenerator;
  47.   long pLow;
  48.   long pHigh;
  49.  
  50.   long _asLong(long, long);
  51.  
  52.  public:
  53.  
  54.        RandomInteger(long low, long high, RNG *gen);
  55.        RandomInteger(long high, RNG *gen);
  56.        RandomInteger(RNG *gen);
  57.  
  58. // read params
  59.  
  60.   long low() const;
  61.   long high() const;
  62.   RNG* generator() const;
  63.  
  64. // change params
  65.  
  66.   long low(long x);
  67.   long high(long x);
  68.   RNG* generator(RNG *gen);
  69.  
  70. // get a random number
  71.  
  72.   long asLong();
  73.   long operator()(); // synonym for asLong
  74.   int  asInt();      // (possibly) truncate as int
  75.  
  76. // override params for one shot
  77.  
  78.   long asLong(long high);
  79.   long asLong(long low, long high);
  80.  
  81.   long operator () (long high);  // synonyms
  82.   long operator () (long low, long high);
  83.  
  84. };
  85.  
  86. inline RandomInteger::RandomInteger(long low, long high, RNG *gen) 
  87.      : pLow((low < high) ? low : high),
  88.        pHigh((low < high) ? high : low),
  89.        pGenerator(gen)
  90. {}
  91.  
  92. inline RandomInteger::RandomInteger(long high, RNG *gen) 
  93.      : pLow((0 < high) ? 0 : high),
  94.        pHigh((0 < high) ? high : 0),
  95.        pGenerator(gen)
  96. {}
  97.   
  98.  
  99. inline RandomInteger::RandomInteger(RNG *gen) 
  100.      : pLow(0),
  101.        pHigh(1),
  102.        pGenerator(gen)
  103. {}
  104.  
  105. inline RNG* RandomInteger::generator() const { return pGenerator;}
  106. inline long RandomInteger::low() const       { return pLow; }
  107. inline long RandomInteger::high() const      { return pHigh; }
  108.  
  109. inline RNG* RandomInteger::generator(RNG *gen) 
  110. {
  111.   RNG *tmp = pGenerator; pGenerator = gen;  return tmp;
  112. }
  113.  
  114. inline long RandomInteger::low(long x)  
  115. {
  116.   long tmp = pLow;  pLow = x;  return tmp;
  117. }
  118.  
  119. inline long RandomInteger:: high(long x) 
  120. {
  121.   long tmp = pHigh; pHigh = x; return tmp;
  122. }
  123.  
  124. inline long RandomInteger:: _asLong(long low, long high)
  125. {    
  126.   return (pGenerator->asLong() % (high-low+1)) + low;
  127. }
  128.  
  129.  
  130. inline long RandomInteger:: asLong() 
  131. {
  132.   return _asLong(pLow, pHigh);
  133. }
  134.  
  135. inline long RandomInteger:: asLong(long high)
  136. {
  137.   return _asLong(pLow, high);
  138. }
  139.  
  140. inline long RandomInteger:: asLong(long low, long high)
  141. {
  142.   return _asLong(low, high);
  143. }
  144.  
  145. inline long RandomInteger:: operator () () 
  146. {
  147.   return _asLong(pLow, pHigh);
  148. }
  149.  
  150. inline long RandomInteger:: operator () (long high)
  151. {
  152.   return _asLong(pLow, high);
  153. }
  154.  
  155. inline long RandomInteger:: operator () (long low, long high)
  156. {
  157.   return _asLong(low, high);
  158. }
  159.  
  160.  
  161.  
  162.  
  163. inline int RandomInteger:: asInt() 
  164. {
  165.   return int(asLong());
  166. }
  167.  
  168. #endif
  169.