mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: cachedRandom - added 'shuffle' function to shuffle list inplace
BUG: cachedRandom - updated how generator is (re)initialied
This commit is contained in:
@ -66,18 +66,19 @@ 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;
|
||||
}
|
||||
forAll(samples_, i)
|
||||
{
|
||||
samples_[i] = osRandomDouble();
|
||||
}
|
||||
|
||||
// Initialise samples
|
||||
osRandomSeed(seed_);
|
||||
forAll(samples_, i)
|
||||
{
|
||||
samples_[i] = osRandomDouble();
|
||||
sampleI_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,23 +91,28 @@ 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())
|
||||
{
|
||||
sampleI_ = 0;
|
||||
if (samples_.size())
|
||||
{
|
||||
sampleI_ = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Re-initialise the samples
|
||||
osRandomSeed(seed_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user