Sync usage with man page.
[netbsd-mini2440.git] / gnu / lib / libg++ / g++-include / RndInt.h
blobf674796f05848ee00b4daaa819b71db757fd60c2
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>
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.
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 and this notice must be preserved on all copies.
23 #ifndef _RandomInteger_h
24 #ifdef __GNUG__
25 #pragma once
26 #pragma interface
27 #endif
28 #define _RandomInteger_h 1
30 // RandomInteger uses a random number generator to generate an integer
31 // in a specified range. By default the range is 0..1. Since in my
32 // experience random numbers are often needed for a wide variety of
33 // ranges in the same program, this generator accepts a new low or high value
34 // as an argument to the asLong and operator() methods to temporarily
35 // override stored values
37 #include <math.h>
38 #include "RNG.h"
40 class RandomInteger
42 protected:
43 RNG *pGenerator;
44 long pLow;
45 long pHigh;
47 long _asLong(long, long);
49 public:
51 RandomInteger(long low, long high, RNG *gen);
52 RandomInteger(long high, RNG *gen);
53 RandomInteger(RNG *gen);
55 // read params
57 long low() const;
58 long high() const;
59 RNG* generator() const;
61 // change params
63 long low(long x);
64 long high(long x);
65 RNG* generator(RNG *gen);
67 // get a random number
69 long asLong();
70 long operator()(); // synonym for asLong
71 int asInt(); // (possibly) truncate as int
73 // override params for one shot
75 long asLong(long high);
76 long asLong(long low, long high);
78 long operator () (long high); // synonyms
79 long operator () (long low, long high);
83 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
85 inline RandomInteger::RandomInteger(long low, long high, RNG *gen)
86 : pLow((low < high) ? low : high),
87 pHigh((low < high) ? high : low),
88 pGenerator(gen)
91 inline RandomInteger::RandomInteger(long high, RNG *gen)
92 : pLow((0 < high) ? 0 : high),
93 pHigh((0 < high) ? high : 0),
94 pGenerator(gen)
98 inline RandomInteger::RandomInteger(RNG *gen)
99 : pLow(0),
100 pHigh(1),
101 pGenerator(gen)
104 inline RNG* RandomInteger::generator() const { return pGenerator;}
105 inline long RandomInteger::low() const { return pLow; }
106 inline long RandomInteger::high() const { return pHigh; }
108 inline RNG* RandomInteger::generator(RNG *gen)
110 RNG *tmp = pGenerator; pGenerator = gen; return tmp;
113 inline long RandomInteger::low(long x)
115 long tmp = pLow; pLow = x; return tmp;
118 inline long RandomInteger:: high(long x)
120 long tmp = pHigh; pHigh = x; return tmp;
123 inline long RandomInteger:: _asLong(long low, long high)
125 return (pGenerator->asLong() % (high-low+1)) + low;
129 inline long RandomInteger:: asLong()
131 return _asLong(pLow, pHigh);
134 inline long RandomInteger:: asLong(long high)
136 return _asLong(pLow, high);
139 inline long RandomInteger:: asLong(long low, long high)
141 return _asLong(low, high);
144 inline long RandomInteger:: operator () ()
146 return _asLong(pLow, pHigh);
149 inline long RandomInteger:: operator () (long high)
151 return _asLong(pLow, high);
154 inline long RandomInteger:: operator () (long low, long high)
156 return _asLong(low, high);
162 inline int RandomInteger:: asInt()
164 return int(asLong());
167 #endif
169 #endif