Transferred copyright to the OpenFOAM Foundation
[OpenFOAM-2.0.x.git] / src / OpenFOAM / primitives / random / cachedRandom / cachedRandom.C
blob4bf4054deea1a2c1d86f6bbce7bf3f09a8249f6e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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!"
32 #endif
34 // * * * * * * * * * * * * * private Member Functions  * * * * * * * * * * * //
36 Foam::scalar Foam::cachedRandom::scalar01()
38     if (sampleI_ < 0)
39     {
40         return osRandomDouble();
41     }
43     if (sampleI_ == samples_.size() - 1)
44     {
45         scalar s = samples_[sampleI_];
46         sampleI_ = 0;
47         return s;
48     }
49     else
50     {
51         scalar s = samples_[sampleI_];
52         sampleI_++;
53         return s;
54     }
58 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
60 Foam::cachedRandom::cachedRandom(const label seed, const label count)
62     seed_(1),
63     samples_(0),
64     sampleI_(-1)
66     if (seed > 1)
67     {
68         seed_ = seed;
69     }
71     // Samples will be cached if count > 0
72     if (count > 0)
73     {
74         samples_.setSize(count);
75         sampleI_ = 0;
76     }
78     // Initialise samples
79     osRandomSeed(seed_);
80     forAll(samples_, i)
81     {
82         samples_[i] = drand48();
83     }
87 Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
89     seed_(cr.seed_),
90     samples_(cr.samples_),
91     sampleI_(cr.sampleI_)
93     if (sampleI_ == -1)
94     {
95         WarningIn
96         (
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;
101         osRandomSeed(seed_);
102     }
103     else if (reset)
104     {
105         sampleI_ = 0;
106     }
110 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
112 Foam::cachedRandom::~cachedRandom()
116 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
118 template<>
119 Foam::label Foam::cachedRandom::sample01()
121     return round(scalar01());
125 template<>
126 Foam::scalar Foam::cachedRandom::sample01()
128     return scalar01();
132 template<>
133 Foam::label Foam::cachedRandom::position(const label& start, const label& end)
135     return start + round(scalar01()*(end - start));
139 template<>
140 Foam::scalar Foam::cachedRandom::position
142     const scalar& start,
143     const scalar& end
146     return start + scalar01()*(end - start);
150 void Foam::cachedRandom::operator=(const cachedRandom& cr)
152     seed_ = cr.seed_;
153     samples_ = cr.samples_;
154     sampleI_ = cr.sampleI_;
158 // ************************************************************************* //