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 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::Random
29 
30 Description
31  Random number generator.
32 
33 SourceFiles
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 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 template<class T> class UList;
55 
56 /*---------------------------------------------------------------------------*\
57  Class Random Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 class 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 
86 public:
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 
170 template<>
171 scalar Random::sample01<scalar>();
172 
173 template<>
174 label Random::sample01<label>();
175 
176 template<>
177 scalar Random::GaussNormal<scalar>();
178 
179 template<>
180 label Random::GaussNormal<label>();
181 
182 template<>
183 scalar Random::position<scalar>(const scalar& start, const scalar& end);
184 
185 template<>
186 label Random::position<label>(const label& start, const label& end);
187 
188 template<>
189 scalar Random::globalSample01<scalar>();
190 
191 template<>
192 label Random::globalSample01<label>();
193 
194 template<>
195 scalar Random::globalGaussNormal<scalar>();
196 
197 template<>
198 label Random::globalGaussNormal<label>();
199 
200 template<>
201 scalar Random::globalPosition<scalar>(const scalar& start, const scalar& end);
202 
203 template<>
204 label 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()
215 template<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 
237 public:
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()
287 template<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 
302 public:
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 // ************************************************************************* //
Foam::Random::defaultSeed
static constexpr label defaultSeed
The default seed value (name may change in the future)
Definition: Random.H:95
Foam::Random
Random number generator.
Definition: Random.H:59
Foam::Random::globalSample01
Type globalSample01()
Return a sample whose components lie in the range [0,1].
Definition: RandomTemplates.C:94
Rand48.H
Foam::Random::GaussNormal
Type GaussNormal()
Definition: RandomTemplates.C:49
RandomI.H
Foam::Random::uniformGeneratorOp
Definition: Random.H:88
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::Random::gaussianGeneratorOp
A generator class returning a gaussian distributed random number.
Definition: Random.H:89
Foam::Random::sample01
Type sample01()
Return a sample whose components lie in the range [0,1].
Definition: RandomTemplates.C:36
Foam::Random::uniformGeneratorOp::uniformGeneratorOp
uniformGeneratorOp(const label seed, const T &minval, const T &maxval)
Construct with seed value and specified interval.
Definition: Random.H:253
Foam::Random::uniformGeneratorOp::operator()
T operator()(const U &) const
Definition: Random.H:272
Foam::Random::shuffle
void shuffle(UList< Type > &values)
Shuffle the values in the list.
Definition: RandomTemplates.C:83
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::Random::Random
Random(const label seedValue=defaultSeed)
Construct with seed value.
Definition: Random.C:33
Foam::Random::randomise01
void randomise01(Type &value)
Randomise value in the range [0,1].
Definition: RandomTemplates.C:76
Foam::Random::reset
void reset(const label seedValue)
Reset the random number generator seed.
Definition: RandomI.H:50
Foam::Random::uniformGeneratorOp::uniformGeneratorOp
uniformGeneratorOp(const label seed=Random::defaultSeed)
Default construct or with seed value. Uses default [0,1] interval.
Definition: Random.H:241
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Random::seed
label seed() const
The initial random number seed that was used.
Definition: RandomI.H:44
RandomTemplates.C
scalar.H
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
U
U
Definition: pEqn.H:72
Foam::Random::globalRandomise01
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Definition: RandomTemplates.C:142
Foam::Random::globalGaussNormal
Type globalGaussNormal()
Definition: RandomTemplates.C:110
Foam::Random::position
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
Definition: RandomTemplates.C:62
label.H
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::UList< Type >
Foam::Random::uniformGeneratorOp::uniformGeneratorOp
uniformGeneratorOp(const T &minval, const T &maxval)
Construct with specified interval, using default seed.
Definition: Random.H:247
Foam::Random::gaussianGeneratorOp::operator()
T operator()()
Generate a random number.
Definition: Random.H:315
Foam::Random::bit
int bit()
Return a random bit.
Definition: RandomI.H:38
Foam::Rand48
A pseudo random number generator using the linear congruential algorithm with the following parameter...
Definition: Rand48.H:59
Foam::Random::uniformGeneratorOp::operator()
T operator()()
Generate a random number.
Definition: Random.H:264
Foam::Random::gaussianGeneratorOp::gaussianGeneratorOp
gaussianGeneratorOp(const label seed=Random::defaultSeed)
Default construct or with seed value. Uses default [0,1] interval.
Definition: Random.H:306
Foam::Random::globalPosition
Type globalPosition(const Type &start, const Type &end)
Return a sample on the interval [start,end].
Definition: RandomTemplates.C:126
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::Random::gaussianGeneratorOp::operator()
T operator()(const U &) const
Definition: Random.H:323