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 // * * * * * * * * * * * * * private Member Functions * * * * * * * * * * * //
31 Foam::scalar Foam::cachedRandom::scalar01()
35 return osRandomDouble();
38 if (sampleI_ == samples_.size() - 1)
40 scalar s = samples_[sampleI_];
46 scalar s = samples_[sampleI_];
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 Foam::cachedRandom::cachedRandom(const label seed, const label count)
66 // Samples will be cached if count > 0
69 samples_.setSize(count);
77 samples_[i] = osRandomDouble();
82 Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
85 samples_(cr.samples_),
92 "Foam::cachedRandom::cachedRandom(const cachedRandom& cr)"
93 ) << "Copy constructor called, but samples not being cached. "
94 << "This may lead to non-repeatable behaviour" << endl;
105 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
107 Foam::cachedRandom::~cachedRandom()
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
114 Foam::label Foam::cachedRandom::sample01()
116 return round(scalar01());
121 Foam::scalar Foam::cachedRandom::sample01()
128 Foam::label Foam::cachedRandom::position(const label& start, const label& end)
130 return start + round(scalar01()*(end - start));
135 Foam::scalar Foam::cachedRandom::position
141 return start + scalar01()*(end - start);
145 void Foam::cachedRandom::operator=(const cachedRandom& cr)
148 samples_ = cr.samples_;
149 sampleI_ = cr.sampleI_;
153 // ************************************************************************* //