1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 #if INT_MAX != 2147483647
37 # error "INT_MAX != 2147483647"
38 # error "The random number generator may not work!"
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 // construct given seed
51 Random::Random(const label& seed)
63 srandom((unsigned int)Seed);
74 if (random() > INT_MAX/2)
76 if (lrand48() > INT_MAX/2)
88 scalar Random::scalar01()
91 return (scalar)random()/INT_MAX;
98 vector Random::vector01()
101 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
103 rndVec.component(cmpt) = scalar01();
110 sphericalTensor Random::sphericalTensor01()
112 sphericalTensor rndTen;
113 rndTen.ii() = scalar01();
119 symmTensor Random::symmTensor01()
122 for (direction cmpt=0; cmpt<symmTensor::nComponents; cmpt++)
124 rndTen.component(cmpt) = scalar01();
131 tensor Random::tensor01()
134 for (direction cmpt=0; cmpt<tensor::nComponents; cmpt++)
136 rndTen.component(cmpt) = scalar01();
143 label Random::integer(const label lower, const label upper)
146 return lower + (random() % (upper+1-lower));
148 return lower + (lrand48() % (upper+1-lower));
153 vector Random::position(const vector& start, const vector& end)
155 vector rndVec(start);
157 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
159 rndVec.component(cmpt) +=
160 scalar01()*(end.component(cmpt) - start.component(cmpt));
167 void Random::randomise(scalar& s)
173 void Random::randomise(vector& v)
179 void Random::randomise(sphericalTensor& st)
181 st = sphericalTensor01();
185 void Random::randomise(symmTensor& st)
191 void Random::randomise(tensor& t)
197 // return a normal Gaussian randon number
198 // with zero mean and unity variance N(0, 1)
200 scalar Random::GaussNormal()
204 scalar fac, rsq, v1, v2;
210 v1 = 2.0*scalar01() - 1.0;
211 v2 = 2.0*scalar01() - 1.0;
213 } while (rsq >= 1.0 || rsq == 0.0);
215 fac = sqrt(-2.0 * log(rsq)/rsq);
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 } // End namespace Foam
234 // ************************************************************************* //