1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Random number generator.
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.
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
38 Note: the copy constructor cannot be used if count = -1.
43 cachedRandomTemplates.C
45 \*---------------------------------------------------------------------------*/
47 #ifndef cachedRandom_H
48 #define cachedRandom_H
50 #include "scalarList.H"
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 // Forward declaration of classes
60 /*---------------------------------------------------------------------------*\
61 Class cachedRandom Declaration
62 \*---------------------------------------------------------------------------*/
70 //- Initial random number seed
73 //- List of scalar samples
76 //- Current sample marker
80 // Private Member Functions
82 //- Returns the current sample
91 //- Construct given seed and sample count
92 cachedRandom(const label seed, const label count);
94 //- Copy constructor with optional reset of sampleI
95 cachedRandom(const cachedRandom& cr, const bool reset = false);
106 //- Return const access to the initial random number seed
107 inline label seed() const;
109 //- Return const access to the list of samples
110 inline const scalarList& samples() const;
112 //- Return the current sample marker
113 inline label sampleI() const;
118 //- Return non-const access to the sample marker
119 inline label& sampleI();
124 //- Return a sample whose components lie in the range 0-1
128 //- Return a sample between start and end
130 Type position(const Type& start, const Type& end);
132 //- Randomise value in the range 0-1
134 void randomise01(Type& value);
139 //- Assignment operator
140 void operator=(const cachedRandom& cr);
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
145 // Template specialisations
148 label cachedRandom::sample01<label>();
151 scalar cachedRandom::sample01<scalar>();
154 label cachedRandom::position<label>(const label& start, const label& end);
157 scalar cachedRandom::position<scalar>
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
165 } // End namespace Foam
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 #include "cachedRandomI.H"
171 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 # include "cachedRandomTemplates.C"
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
181 // ************************************************************************* //