ENH: cachedRandom - added 'shuffle' function to shuffle list inplace

BUG: cachedRandom - updated how generator is (re)initialied
This commit is contained in:
Andrew Heather
2016-06-28 13:55:58 +01:00
parent b3bea42ada
commit c233552dda
3 changed files with 48 additions and 21 deletions

View File

@ -66,18 +66,19 @@ Foam::cachedRandom::cachedRandom(const label seed, const label count)
seed_ = seed; seed_ = seed;
} }
// Initialise samples
osRandomSeed(seed_);
// Samples will be cached if count > 0 // Samples will be cached if count > 0
if (count > 0) if (count > 0)
{ {
samples_.setSize(count); samples_.setSize(count);
sampleI_ = 0; forAll(samples_, i)
} {
samples_[i] = osRandomDouble();
}
// Initialise samples sampleI_ = 0;
osRandomSeed(seed_);
forAll(samples_, i)
{
samples_[i] = osRandomDouble();
} }
} }
@ -90,23 +91,28 @@ Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
hasGaussSample_(cr.hasGaussSample_), hasGaussSample_(cr.hasGaussSample_),
gaussSample_(cr.gaussSample_) gaussSample_(cr.gaussSample_)
{ {
//if (sampleI_ == -1)
//{
// WarningInFunction
// << "Copy constructor called, but samples not being cached. "
// << "This may lead to non-repeatable behaviour" << endl;
//
//}
if (reset) if (reset)
{ {
hasGaussSample_ = false; hasGaussSample_ = false;
gaussSample_ = 0; gaussSample_ = 0;
}
if (sampleI_ == -1)
{
WarningInFunction
<< "Copy constructor called, but samples not being cached. "
<< "This may lead to non-repeatable behaviour" << endl;
osRandomSeed(seed_); if (samples_.size())
} {
sampleI_ = 0;
if (reset && samples_.size()) }
{ else
sampleI_ = 0; {
// Re-initialise the samples
osRandomSeed(seed_);
}
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -143,6 +143,10 @@ public:
template<class Type> template<class Type>
void randomise01(Type& value); void randomise01(Type& value);
//- Shuffle the values in the list
template<class Type>
void shuffle(UList<Type>& values);
// Global random numbers - consistent across all processors // Global random numbers - consistent across all processors
@ -224,7 +228,7 @@ label cachedRandom::globalPosition<label>(const label& start, const label& end);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository #ifdef NoRepository
#include "cachedRandomTemplates.C" # include "cachedRandomTemplates.C"
#endif #endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -75,6 +75,23 @@ void Foam::cachedRandom::randomise01(Type& value)
} }
template<class Type>
void Foam::cachedRandom::shuffle(UList<Type>& values)
{
const label nSample = values.size();
label posI = nSample - 1;
for (label i = 1; i < nSample; i++)
{
label j = position<label>(0, posI);
Type t = values[j];
values[j] = values[posI];
values[posI] = t;
posI--;
}
}
template<class Type> template<class Type>
Type Foam::cachedRandom::globalSample01() Type Foam::cachedRandom::globalSample01()
{ {