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,19 +66,20 @@ Foam::cachedRandom::cachedRandom(const label seed, const label count)
seed_ = seed;
}
// Initialise samples
osRandomSeed(seed_);
// Samples will be cached if count > 0
if (count > 0)
{
samples_.setSize(count);
sampleI_ = 0;
}
// Initialise samples
osRandomSeed(seed_);
forAll(samples_, i)
{
samples_[i] = osRandomDouble();
}
sampleI_ = 0;
}
}
@ -90,24 +91,29 @@ Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
hasGaussSample_(cr.hasGaussSample_),
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)
{
hasGaussSample_ = false;
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 (reset && samples_.size())
if (samples_.size())
{
sampleI_ = 0;
}
else
{
// Re-initialise the samples
osRandomSeed(seed_);
}
}
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -143,6 +143,10 @@ public:
template<class Type>
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
@ -224,7 +228,7 @@ label cachedRandom::globalPosition<label>(const label& start, const label& end);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "cachedRandomTemplates.C"
# include "cachedRandomTemplates.C"
#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>
Type Foam::cachedRandom::globalSample01()
{