cachedRandom.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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::cachedRandom
26 
27 Description
28  Random number generator.
29 
30  Pre-computes and caches samples on construction, so that when sample01()
31  is called, the function simply returns the next (pre-computed) sample. On
32  reaching the last sample, the sample sequence is repeated.
33 
34  Constructed using a seed and sample count. If the supplied count is
35  negative, no caching is performed, and a new sample is generated on each
36  call to sample01().
37 
38  Note: the copy constructor cannot be used if count = -1.
39 
40 SourceFiles
41  cachedRandomI.H
42  cachedRandom.C
43  cachedRandomTemplates.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef cachedRandom_H
48 #define cachedRandom_H
49 
50 #include "scalarList.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward declaration of classes
58 class cachedRandom;
59 
60 /*---------------------------------------------------------------------------*\
61  Class cachedRandom Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class cachedRandom
65 {
66  // Private data
67 
68  //- Initial random number seed
69  label seed_;
70 
71  //- List of scalar samples
72  scalarList samples_;
73 
74  //- Current sample marker
75  label sampleI_;
76 
77  //- Indicator, which tells if there is a stored gaussian sample
78  bool hasGaussSample_;
79 
80  //- Stored sample value
81  scalar gaussSample_;
82 
83 
84  // Private Member Functions
85 
86  //- Returns the current sample
87  scalar scalar01();
88 
89 
90 public:
91 
92  // Constructors
93 
94  //- Construct given seed and sample count
95  cachedRandom(const label seed, const label count);
96 
97  //- Copy constructor with optional reset of sampleI
98  cachedRandom(const cachedRandom& cr, const bool reset = false);
99 
100 
101  // Destructor
102  ~cachedRandom();
103 
104 
105  // Member functions
106 
107  // Access
108 
109  //- Return const access to the initial random number seed
110  inline label seed() const;
111 
112  //- Return const access to the list of samples
113  inline const scalarList& samples() const;
114 
115  //- Return the current sample marker
116  inline label sampleI() const;
117 
118 
119  // Manipulation
120 
121  //- Return non-const access to the sample marker
122  inline label& sampleI();
123 
124 
125  // Evaluation
126 
127  // Random numbers
128 
129  //- Return a sample whose components lie in the range 0-1
130  template<class Type>
131  Type sample01();
132 
133  //- Return a sample whose components are normally distributed
134  // with zero mean and unity variance N(0, 1)
135  template<class Type>
136  Type GaussNormal();
137 
138  //- Return a sample between start and end
139  template<class Type>
140  Type position(const Type& start, const Type& end);
141 
142  //- Randomise value in the range 0-1
143  template<class Type>
144  void randomise01(Type& value);
145 
146 
147  // Global random numbers - consistent across all processors
148 
149  //- Return a sample whose components lie in the range 0-1
150  template<class Type>
151  Type globalSample01();
152 
153  //- Return a sample whose components are normally distributed
154  // with zero mean and unity variance N(0, 1)
155  template<class Type>
156  Type globalGaussNormal();
157 
158  //- Return a sample between start and end
159  template<class Type>
160  Type globalPosition(const Type& start, const Type& end);
161 
162  //- Randomise value in the range 0-1
163  template<class Type>
164  void globalRandomise01(Type& value);
165 };
166 
167 
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 // Template specialisations
170 
171 template<>
172 scalar cachedRandom::sample01<scalar>();
173 
174 template<>
175 label cachedRandom::sample01<label>();
176 
177 template<>
178 scalar cachedRandom::GaussNormal<scalar>();
179 
180 template<>
181 label cachedRandom::GaussNormal<label>();
182 
183 template<>
184 scalar cachedRandom::position<scalar>
185 (
186  const scalar& start,
187  const scalar& end
188 );
189 
190 template<>
191 label cachedRandom::position<label>(const label& start, const label& end);
192 
193 template<>
194 scalar cachedRandom::globalSample01<scalar>();
195 
196 template<>
197 label cachedRandom::globalSample01<label>();
198 
199 template<>
200 scalar cachedRandom::globalGaussNormal<scalar>();
201 
202 template<>
203 label cachedRandom::globalGaussNormal<label>();
204 
205 template<>
206 scalar cachedRandom::globalPosition<scalar>
207 (
208  const scalar& start,
209  const scalar& end
210 );
211 
212 template<>
213 label cachedRandom::globalPosition<label>(const label& start, const label& end);
214 
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 } // End namespace Foam
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 #include "cachedRandomI.H"
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 #ifdef NoRepository
227  #include "cachedRandomTemplates.C"
228 #endif
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 
232 #endif
233 
234 // ************************************************************************* //
label seed() const
Return const access to the initial random number seed.
Definition: cachedRandomI.H:30
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
cachedRandom(const label seed, const label count)
Construct given seed and sample count.
Definition: cachedRandom.C:56
Type globalPosition(const Type &start, const Type &end)
Return a sample between start and end.
Random number generator.
Definition: cachedRandom.H:63
Type GaussNormal()
Return a sample whose components are normally distributed.
Type sample01()
Return a sample whose components lie in the range 0-1.
void randomise01(Type &value)
Randomise value in the range 0-1.
label sampleI() const
Return the current sample marker.
Definition: cachedRandomI.H:42
const scalarList & samples() const
Return const access to the list of samples.
Definition: cachedRandomI.H:36
Type globalGaussNormal()
Return a sample whose components are normally distributed.
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Type position(const Type &start, const Type &end)
Return a sample between start and end.
Namespace for OpenFOAM.
Type globalSample01()
Return a sample whose components lie in the range 0-1.