1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 #if INT_MAX != 2147483647
40 # error "INT_MAX != 2147483647"
41 # error "The random number generator may not work!"
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 // construct given seed
54 Random::Random(const label& seed)
66 srandom((unsigned int)Seed);
77 if (random() > INT_MAX/2)
79 if (lrand48() > INT_MAX/2)
91 scalar Random::scalar01()
94 return (scalar)random()/INT_MAX;
101 vector Random::vector01()
104 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
106 rndVec.component(cmpt) = scalar01();
113 sphericalTensor Random::sphericalTensor01()
115 sphericalTensor rndTen;
116 rndTen.ii() = scalar01();
122 symmTensor Random::symmTensor01()
125 for (direction cmpt=0; cmpt<symmTensor::nComponents; cmpt++)
127 rndTen.component(cmpt) = scalar01();
134 symmTensor4thOrder Random::symmTensor4thOrder01()
136 symmTensor4thOrder rndTen;
137 for (direction cmpt=0; cmpt<symmTensor4thOrder::nComponents; cmpt++)
139 rndTen.component(cmpt) = scalar01();
146 diagTensor Random::diagTensor01()
149 for (direction cmpt=0; cmpt<diagTensor::nComponents; cmpt++)
151 rndTen.component(cmpt) = scalar01();
158 tensor Random::tensor01()
161 for (direction cmpt=0; cmpt<tensor::nComponents; cmpt++)
163 rndTen.component(cmpt) = scalar01();
170 label Random::integer(const label lower, const label upper)
173 return lower + (random() % (upper+1-lower));
175 return lower + (lrand48() % (upper+1-lower));
180 vector Random::position(const vector& start, const vector& end)
182 vector rndVec(start);
184 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
186 rndVec.component(cmpt) +=
187 scalar01()*(end.component(cmpt) - start.component(cmpt));
194 void Random::randomise(scalar& s)
200 void Random::randomise(vector& v)
206 void Random::randomise(sphericalTensor& st)
208 st = sphericalTensor01();
212 void Random::randomise(symmTensor& st)
218 void Random::randomise(symmTensor4thOrder& st)
220 st = symmTensor4thOrder01();
224 void Random::randomise(diagTensor& dt)
230 void Random::randomise(tensor& t)
236 // return a normal Gaussian randon number
237 // with zero mean and unity variance N(0, 1)
239 scalar Random::GaussNormal()
243 scalar fac, rsq, v1, v2;
249 v1 = 2.0*scalar01() - 1.0;
250 v2 = 2.0*scalar01() - 1.0;
252 } while (rsq >= 1.0 || rsq == 0.0);
254 fac = sqrt(-2.0 * log(rsq)/rsq);
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 } // End namespace Foam
273 // ************************************************************************* //