1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-Random
Description
Simple test for sequence of random numbers
\*---------------------------------------------------------------------------*/
#include "Random.H"
#include <cstdlib>
#include <iostream>
#include <iomanip>
#ifdef __linux__
#define TEST_POSIX_RAND
#define TEST_RAW_IEEE
#endif
// Construct a positive double with the 48 random bits distributed over
// its fractional part so the resulting FP number is [0.0,1.0).
//
// As per glibc erand48() implementation
#ifdef TEST_RAW_IEEE
#include <ieee754.h>
double randomFraction(const uint64_t bits)
{
// 48-bit value
unsigned short int xsubi[3];
xsubi[0] = (bits & 0xffff);
xsubi[1] = ((bits >> 16) & 0xffff);
xsubi[2] = ((bits >> 32) & 0xffff);
union ieee754_double temp;
temp.ieee.negative = 0;
temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
// The lower 4 bits of mantissa1 are always 0.
return temp.d - 1.0;
}
#endif
using namespace Foam;
// Test uniformity of random
void testPosition(const label n)
{
List<label> samples(n, Zero);
Random rnd(123456);
for (label i=0; i < 100000*n; ++i)
{
++samples[rnd.position<label>(0,n-1)];
}
Info<< nl << "uniform [0," << n << ")\n "
<< flatOutput(samples) << nl;
}
// Output with cout instead of Info to retain unsigned values on output
using std::cout;
using std::setw;
#define PutValue(arg) \
cout<< setw(12) << (arg);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
// Allow multiple passes to ensure reset is working properly
const label maxIter = 1;
std::default_random_engine deflt(123456);
std::mt19937 mtwist(123456);
std::uniform_real_distribution<scalar> uniform01;
{
Rand48 rnd(123456);
for (label iter=0; iter < maxIter; ++iter)
{
rnd.seed(123456);
#ifdef TEST_POSIX_RAND
::srand48(123456);
#endif
mtwist.seed(123456);
cout<< nl << "32-bit random with seed = 123456" << nl;
PutValue("Rand48()");
#ifdef TEST_POSIX_RAND
PutValue("lrand48()");
#endif
PutValue("mtwister");
PutValue("default");
cout<< nl;
for (int i=0; i<25; i++)
{
PutValue(rnd());
#ifdef TEST_POSIX_RAND
PutValue(long(::lrand48()));
#endif
PutValue(long(mtwist()));
PutValue(long(deflt()));
cout<< nl;
}
}
}
{
Random rnd(123456);
Rand48 manual(123456);
mtwist.seed(123456);
deflt.seed(123456);
// Two passes to ensure that reset is working properly
for (label iter=0; iter < maxIter; ++iter)
{
#ifdef TEST_POSIX_RAND
::srand48(123456);
#endif
rnd.reset(123456);
manual.seed(123456);
mtwist.seed(123456);
deflt.seed(123456);
cout<< nl << "Random (Rand48) with seed = " << rnd.seed()
<< " interval [0,1000]" << nl;
PutValue("Rand48()");
#ifdef TEST_RAW_IEEE
PutValue("manual");
#endif
PutValue("mtwister");
cout<< nl;
for (int i=0; i<25; i++)
{
PutValue(rnd.sample01<scalar>()*1000);
#ifdef TEST_POSIX_RAND
PutValue(drand48()*1000);
#endif
#ifdef TEST_RAW_IEEE
PutValue(randomFraction(manual.raw())*1000);
#endif
PutValue(uniform01(mtwist)*1000);
PutValue(uniform01(deflt)*1000);
cout<< nl;
}
}
}
{
Rand48 rnd1(123456);
Rand48 rnd2(123456);
#ifdef TEST_POSIX_RAND
::srand48(123456);
#endif
rnd2.discard(10);
cout<< nl << "Rand48 - test with offset of 10" << nl;
PutValue("Rand48()");
PutValue("offset-10");
#ifdef TEST_POSIX_RAND
PutValue("lrand48()");
#endif
cout<< nl;
for (int i=0; i<25; i++)
{
PutValue(rnd1());
PutValue(rnd2());
#ifdef TEST_POSIX_RAND
PutValue(long(::lrand48()));
#endif
cout<< nl;
}
}
// Test uniformity of random
testPosition(20);
testPosition(3);
// Generators
{
const label n = 20;
List<label> samples(n, Zero);
Random::uniformGeneratorOp<label> gen(0, n-1);
// Test uniformity of random
samples = Zero;
for (label i=0; i < 100000*n; ++i)
{
// Calling with/without parameter is the same
if (i % 3)
{
++samples[gen()];
}
else
{
// Unary ignores any parameter
++samples[gen(3.14159)];
}
}
Info<< nl << "Uniform generator [0," << n << ")\n "
<< flatOutput(samples) << nl;
}
{
Random::gaussianGeneratorOp<scalar> gen;
Info<< "Some gaussian generated values" << nl;
for (label i=0; i < 20; ++i)
{
Info<< ' ';
// Calling with/without parameter is the same
if (i % 3)
{
Info<< gen();
}
else
{
// Unary ignores any parameter
Info<< gen(3.14159);
}
}
Info<< nl;
}
// This should fail (in FULLDEBUG)
const bool throwingError = FatalError.throwExceptions();
try
{
Info<<"Random position(10,5): "
<< Random().position<label>(10, 5) << endl;
}
catch (const Foam::error& err)
{
Info<< "Caught FatalError " << err << nl << endl;
}
FatalError.throwExceptions(throwingError);
Info<< "\nDone" << nl << endl;
return 0;
}
// ************************************************************************* //
|