NEURON
RndInt.h
Go to the documentation of this file.
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, 675 Mass Ave, Cambridge, MA 02139, 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 <
RNG.h
>
48
49
class
RandomInteger
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
:
pLow
((low < high) ? low : high),
95
pHigh
((low < high) ? high : low),
96
pGenerator
(gen)
97
{}
98
99
inline
RandomInteger::RandomInteger
(
long
high
,
RNG
*gen)
100
:
pLow
((0 < high) ? 0 : high),
101
pHigh
((0 < high) ? high : 0),
102
pGenerator
(gen)
103
{}
104
105
106
inline
RandomInteger::RandomInteger
(
RNG
*gen)
107
:
pLow
(0),
108
pHigh
(1),
109
pGenerator
(gen)
110
{}
111
112
inline
RNG
*
RandomInteger::generator
()
const
{
return
pGenerator
;}
113
inline
long
RandomInteger::low
()
const
{
return
pLow
; }
114
inline
long
RandomInteger::high
()
const
{
return
pHigh
; }
115
116
inline
RNG
*
RandomInteger::generator
(
RNG
*gen)
117
{
118
RNG
*tmp =
pGenerator
;
pGenerator
= gen;
return
tmp;
119
}
120
121
inline
long
RandomInteger::low
(
long
x)
122
{
123
long
tmp =
pLow
;
pLow
= x;
return
tmp;
124
}
125
126
inline
long
RandomInteger:: high
(
long
x)
127
{
128
long
tmp =
pHigh
;
pHigh
= x;
return
tmp;
129
}
130
131
inline
long
RandomInteger:: _asLong
(
long
low
,
long
high
)
132
{
133
return
(
pGenerator
->
asLong
() % (high-low+1)) +
low
;
134
}
135
136
137
inline
long
RandomInteger:: asLong
()
138
{
139
return
_asLong
(
pLow
,
pHigh
);
140
}
141
142
inline
long
RandomInteger:: asLong
(
long
high
)
143
{
144
return
_asLong
(
pLow
, high);
145
}
146
147
inline
long
RandomInteger:: asLong
(
long
low
,
long
high
)
148
{
149
return
_asLong
(low, high);
150
}
151
152
inline
long
RandomInteger:: operator ()
()
153
{
154
return
_asLong
(
pLow
,
pHigh
);
155
}
156
157
inline
long
RandomInteger:: operator ()
(
long
high
)
158
{
159
return
_asLong
(
pLow
, high);
160
}
161
162
inline
long
RandomInteger:: operator ()
(
long
low
,
long
high
)
163
{
164
return
_asLong
(low, high);
165
}
166
167
168
169
170
inline
int
RandomInteger:: asInt
()
171
{
172
return
int
(
asLong
());
173
}
174
175
#endif
math.h
RandomInteger::operator()
long operator()()
Definition:
RndInt.h:152
RandomInteger::asInt
int asInt()
Definition:
RndInt.h:170
RNG
Definition:
RNG.h:55
RandomInteger::pHigh
long pHigh
Definition:
RndInt.h:54
RandomInteger::pLow
long pLow
Definition:
RndInt.h:53
RNG::asLong
virtual uint32_t asLong()=0
RNG.h
RandomInteger::pGenerator
RNG * pGenerator
Definition:
RndInt.h:52
int
int
Definition:
nrnmusic.cpp:71
RandomInteger::RandomInteger
RandomInteger(long low, long high, RNG *gen)
Definition:
RndInt.h:93
RandomInteger::high
long high() const
Definition:
RndInt.h:114
RandomInteger::asLong
long asLong()
Definition:
RndInt.h:137
RandomInteger
Definition:
RndInt.h:49
RandomInteger::_asLong
long _asLong(long, long)
Definition:
RndInt.h:131
RandomInteger::low
long low() const
Definition:
RndInt.h:113
RandomInteger::generator
RNG * generator() const
Definition:
RndInt.h:112
src
gnu
RndInt.h