## modules/up/src/Core/gnu/RndInt.h

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. RandomInteger
2. generator
3. low
4. high
5. generator
6. low
7. high
8. _asLong
9. asLong
10. asLong
11. asLong
12. asInt

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

/* [<][>][^][v][top][bottom][index][help] */