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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cachedRandom.H"
27 #include "OSspecific.H"
29 #if INT_MAX != 2147483647
30 # error "INT_MAX != 2147483647"
31 # error "The random number generator may not work!"
34 // * * * * * * * * * * * * * private Member Functions * * * * * * * * * * * //
36 Foam::scalar Foam::cachedRandom::scalar01()
40 return osRandomDouble();
43 if (sampleI_ == samples_.size() - 1)
45 scalar s = samples_[sampleI_];
51 scalar s = samples_[sampleI_];
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 Foam::cachedRandom::cachedRandom(const label seed, const label count)
71 // Samples will be cached if count > 0
74 samples_.setSize(count);
82 samples_[i] = drand48();
87 Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
90 samples_(cr.samples_),
97 "Foam::cachedRandom::cachedRandom(const cachedRandom& cr)"
98 ) << "Copy constructor called, but samples not being cached. "
99 << "This may lead to non-repeatable behaviour" << endl;
110 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
112 Foam::cachedRandom::~cachedRandom()
116 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119 Foam::label Foam::cachedRandom::sample01()
121 return round(scalar01());
126 Foam::scalar Foam::cachedRandom::sample01()
133 Foam::label Foam::cachedRandom::position(const label& start, const label& end)
135 return start + round(scalar01()*(end - start));
140 Foam::scalar Foam::cachedRandom::position
146 return start + scalar01()*(end - start);
150 void Foam::cachedRandom::operator=(const cachedRandom& cr)
153 samples_ = cr.samples_;
154 sampleI_ = cr.sampleI_;
158 // ************************************************************************* //