BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / primitives / random / cachedRandom / cachedRandom.C
blob62f40e5454080032231adfa2bdf74eb6c73a22d2
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 // * * * * * * * * * * * * * private Member Functions  * * * * * * * * * * * //
31 Foam::scalar Foam::cachedRandom::scalar01()
33     if (sampleI_ < 0)
34     {
35         return osRandomDouble();
36     }
38     if (sampleI_ == samples_.size() - 1)
39     {
40         scalar s = samples_[sampleI_];
41         sampleI_ = 0;
42         return s;
43     }
44     else
45     {
46         scalar s = samples_[sampleI_];
47         sampleI_++;
48         return s;
49     }
53 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
55 Foam::cachedRandom::cachedRandom(const label seed, const label count)
57     seed_(1),
58     samples_(0),
59     sampleI_(-1)
61     if (seed > 1)
62     {
63         seed_ = seed;
64     }
66     // Samples will be cached if count > 0
67     if (count > 0)
68     {
69         samples_.setSize(count);
70         sampleI_ = 0;
71     }
73     // Initialise samples
74     osRandomSeed(seed_);
75     forAll(samples_, i)
76     {
77         samples_[i] = osRandomDouble();
78     }
82 Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
84     seed_(cr.seed_),
85     samples_(cr.samples_),
86     sampleI_(cr.sampleI_)
88     if (sampleI_ == -1)
89     {
90         WarningIn
91         (
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;
96         osRandomSeed(seed_);
97     }
98     else if (reset)
99     {
100         sampleI_ = 0;
101     }
105 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
107 Foam::cachedRandom::~cachedRandom()
111 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
113 template<>
114 Foam::label Foam::cachedRandom::sample01()
116     return round(scalar01());
120 template<>
121 Foam::scalar Foam::cachedRandom::sample01()
123     return scalar01();
127 template<>
128 Foam::label Foam::cachedRandom::position(const label& start, const label& end)
130     return start + round(scalar01()*(end - start));
134 template<>
135 Foam::scalar Foam::cachedRandom::position
137     const scalar& start,
138     const scalar& end
141     return start + scalar01()*(end - start);
145 void Foam::cachedRandom::operator=(const cachedRandom& cr)
147     seed_ = cr.seed_;
148     samples_ = cr.samples_;
149     sampleI_ = cr.sampleI_;
153 // ************************************************************************* //