Random.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2017-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::Random
29
30Description
31 Random number generator.
32
33SourceFiles
34 RandomI.H
35 Random.C
36 RandomTemplates.C
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Random_H
41#define Random_H
42
43#include "Rand48.H"
44#include "label.H"
45#include "scalar.H"
46#include <random>
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53// Forward Declarations
54template<class T> class UList;
55
56/*---------------------------------------------------------------------------*\
57 Class Random Declaration
58\*---------------------------------------------------------------------------*/
60class Random
61{
62 // Private Data
63
64 //- Initial random number seed
65 label seed_;
66
67 //- Random number generator on the int32 interval [0,2^31)
68 Rand48 generator_;
69
70 //- Uniform distribution on the scalar interval [0,1]
71 std::uniform_real_distribution<scalar> uniform01_;
72
73 //- Is there a gaussian sample cached?
74 bool hasGaussSample_;
75
76 //- The cached gaussian sample value
77 scalar gaussSample_;
78
79
80 // Private Member Functions
81
82 //- A uniformly distributed floating-point random number [0,1]
83 inline scalar scalar01();
84
85
86public:
87
88 // Forward Declarations - generator classes
89 template<class T> class uniformGeneratorOp;
90 template<class T> class gaussianGeneratorOp;
91
92
93 // Public Static Data
94
95 //- The default seed value (name may change in the future)
96 static constexpr label defaultSeed = 123456;
97
98
99 // Constructors
100
101 //- Construct with seed value
102 explicit Random(const label seedValue = defaultSeed);
103
104 //- Copy construct with possible reset of seed
105 Random(const Random& rnd, const bool reset);
106
107
108 // Member Functions
109
110 // Access
111
112 //- The initial random number seed that was used
113 inline label seed() const;
114
115 //- Reset the random number generator seed.
116 inline void reset(const label seedValue);
117
118
119 // Random numbers
120
121 //- Return a random bit
122 inline int bit();
123
124 //- Return a sample whose components lie in the range [0,1]
125 template<class Type>
126 Type sample01();
127
128 //- Return a sample whose components are normally distributed
129 //- with zero mean and unity variance N(0,1)
130 template<class Type>
131 Type GaussNormal();
132
133 //- Return a sample on the interval [start,end]
134 template<class Type>
135 Type position(const Type& start, const Type& end);
136
137 //- Randomise value in the range [0,1]
138 template<class Type>
139 void randomise01(Type& value);
140
141 //- Shuffle the values in the list
142 template<class Type>
143 void shuffle(UList<Type>& values);
144
145
146 // Global random numbers - consistent across all processors
147
148 //- Return a sample whose components lie in the range [0,1]
149 template<class Type>
150 Type globalSample01();
151
152 //- Return a sample whose components are normally distributed
153 //- with zero mean and unity variance N(0,1)
154 template<class Type>
155 Type globalGaussNormal();
156
157 //- Return a sample on the interval [start,end]
158 template<class Type>
159 Type globalPosition(const Type& start, const Type& end);
160
161 //- Randomise value in the range 0-1
162 template<class Type>
163 void globalRandomise01(Type& value);
164};
165
166
167// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168// Template specialisations
169
170template<>
171scalar Random::sample01<scalar>();
172
173template<>
174label Random::sample01<label>();
175
176template<>
177scalar Random::GaussNormal<scalar>();
178
179template<>
180label Random::GaussNormal<label>();
181
182template<>
183scalar Random::position<scalar>(const scalar& start, const scalar& end);
184
185template<>
186label Random::position<label>(const label& start, const label& end);
187
188template<>
189scalar Random::globalSample01<scalar>();
190
191template<>
192label Random::globalSample01<label>();
193
194template<>
195scalar Random::globalGaussNormal<scalar>();
196
197template<>
198label Random::globalGaussNormal<label>();
199
200template<>
201scalar Random::globalPosition<scalar>(const scalar& start, const scalar& end);
202
203template<>
204label Random::globalPosition<label>(const label& start, const label& end);
205
206
207/*---------------------------------------------------------------------------*\
208 Class Random::uniformGeneratorOp Declaration
209\*---------------------------------------------------------------------------*/
210
211//- A generator class returning a uniformly distributed random number
212//- on the given interval.
213//
214// \sa std::generate()
215template<class T>
217:
218 public Random
219{
220 // Private Data
221
222 //- The interval
223 const T min_;
224 const T max_;
225
226
227 // Private Member Functions
228
229 //- Generate a random number. Treat as mutable.
230 T generate() const
231 {
232 return const_cast<uniformGeneratorOp&>(*this)
233 .position<T>(min_, max_);
234 }
235
236
237public:
238
239 // Constructors
240
241 //- Default construct or with seed value. Uses default [0,1] interval
242 explicit uniformGeneratorOp(const label seed = Random::defaultSeed)
243 :
245 {}
246
247 //- Construct with specified interval, using default seed
248 uniformGeneratorOp(const T& minval, const T& maxval)
249 :
250 uniformGeneratorOp(Random::defaultSeed, minval, maxval)
251 {}
252
253 //- Construct with seed value and specified interval
254 uniformGeneratorOp(const label seed, const T& minval, const T& maxval)
255 :
256 Random(seed),
257 min_(minval),
258 max_(maxval)
259 {}
260
261
262 // Member Operators
263
264 //- Generate a random number
265 T operator()()
266 {
267 return generate();
268 }
269
270 //- Ignore parameter and generate a random number, which allows it
271 //- to be used as a replacement for general unary operators.
272 template<class U>
273 T operator()(const U&) const
274 {
275 return generate();
276 }
277};
278
279
280/*---------------------------------------------------------------------------*\
281 Class Random::gaussianGeneratorOp Declaration
282\*---------------------------------------------------------------------------*/
283
284//- A generator class returning a gaussian distributed random number.
285//
286// \sa std::generate()
287template<class T>
289:
290 public Random
291{
292 // Private Member Functions
293
294 //- Generate a random number. Treat as mutable.
295 T generate() const
296 {
297 return const_cast<gaussianGeneratorOp&>(*this)
298 .GaussNormal<T>();
299 }
300
301
302public:
303
304 // Constructors
305
306 //- Default construct or with seed value. Uses default [0,1] interval
307 explicit gaussianGeneratorOp(const label seed = Random::defaultSeed)
308 :
309 Random(seed)
310 {}
311
312
313 // Member Operators
314
315 //- Generate a random number
316 T operator()()
317 {
318 return generate();
319 }
320
321 //- Ignore parameter and generate a random number, which allows it
322 //- to be used as a replacement for general unary operators.
323 template<class U>
324 T operator()(const U&) const
325 {
326 return generate();
327 }
328};
329
330
331// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
332
333} // End namespace Foam
334
335// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
336
337#include "RandomI.H"
338
339// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
340
341#ifdef NoRepository
342# include "RandomTemplates.C"
343#endif
344
345// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346
347#endif
348
349// ************************************************************************* //
A pseudo random number generator using the linear congruential algorithm with the following parameter...
Definition: Rand48.H:60
A generator class returning a gaussian distributed random number.
Definition: Random.H:290
gaussianGeneratorOp(const label seed=Random::defaultSeed)
Default construct or with seed value. Uses default [0,1] interval.
Definition: Random.H:306
T operator()(const U &) const
Definition: Random.H:323
T operator()()
Generate a random number.
Definition: Random.H:315
uniformGeneratorOp(const label seed=Random::defaultSeed)
Default construct or with seed value. Uses default [0,1] interval.
Definition: Random.H:241
uniformGeneratorOp(const T &minval, const T &maxval)
Construct with specified interval, using default seed.
Definition: Random.H:247
uniformGeneratorOp(const label seed, const T &minval, const T &maxval)
Construct with seed value and specified interval.
Definition: Random.H:253
T operator()(const U &) const
Definition: Random.H:272
T operator()()
Generate a random number.
Definition: Random.H:264
Random number generator.
Definition: Random.H:60
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
int bit()
Return a random bit.
Definition: RandomI.H:38
label seed() const
The initial random number seed that was used.
Definition: RandomI.H:44
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Type GaussNormal()
Type sample01()
Return a sample whose components lie in the range [0,1].
Type globalSample01()
Return a sample whose components lie in the range [0,1].
void shuffle(UList< Type > &values)
Shuffle the values in the list.
void reset(const label seedValue)
Reset the random number generator seed.
Definition: RandomI.H:50
static constexpr label defaultSeed
The default seed value (name may change in the future)
Definition: Random.H:95
Type globalPosition(const Type &start, const Type &end)
Return a sample on the interval [start,end].
void randomise01(Type &value)
Randomise value in the range [0,1].
Type globalGaussNormal()
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
U
Definition: pEqn.H:72
const volScalarField & T
Namespace for OpenFOAM.