mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: Random numbers - cachedRandom - updated to use the re-entrant random interface
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -31,88 +31,38 @@ License
|
||||
|
||||
Foam::scalar Foam::cachedRandom::scalar01()
|
||||
{
|
||||
if (sampleI_ < 0)
|
||||
{
|
||||
return osRandomDouble();
|
||||
}
|
||||
|
||||
if (sampleI_ == samples_.size() - 1)
|
||||
{
|
||||
scalar s = samples_[sampleI_];
|
||||
sampleI_ = 0;
|
||||
return s;
|
||||
}
|
||||
else
|
||||
{
|
||||
scalar s = samples_[sampleI_];
|
||||
sampleI_++;
|
||||
return s;
|
||||
}
|
||||
return osRandomDouble(buffer_);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::cachedRandom::cachedRandom(const label seed, const label count)
|
||||
Foam::cachedRandom::cachedRandom(const label seed)
|
||||
:
|
||||
seed_(1),
|
||||
samples_(0),
|
||||
sampleI_(-1),
|
||||
buffer_(osRandomBufferSize()),
|
||||
seed_(seed),
|
||||
hasGaussSample_(false),
|
||||
gaussSample_(0)
|
||||
{
|
||||
if (seed > 1)
|
||||
{
|
||||
seed_ = seed;
|
||||
}
|
||||
|
||||
// Initialise samples
|
||||
osRandomSeed(seed_);
|
||||
|
||||
// Samples will be cached if count > 0
|
||||
if (count > 0)
|
||||
{
|
||||
samples_.setSize(count);
|
||||
forAll(samples_, i)
|
||||
{
|
||||
samples_[i] = osRandomDouble();
|
||||
}
|
||||
|
||||
sampleI_ = 0;
|
||||
}
|
||||
// Initialise the random number generator
|
||||
osRandomSeed(seed_, buffer_);
|
||||
}
|
||||
|
||||
|
||||
Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
|
||||
:
|
||||
buffer_(cr.buffer_),
|
||||
seed_(cr.seed_),
|
||||
samples_(cr.samples_),
|
||||
sampleI_(cr.sampleI_),
|
||||
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 (samples_.size())
|
||||
{
|
||||
sampleI_ = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Re-initialise the samples
|
||||
osRandomSeed(seed_);
|
||||
}
|
||||
// Re-initialise the samples
|
||||
osRandomSeed(seed_, buffer_);
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,6 +75,13 @@ Foam::cachedRandom::~cachedRandom()
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
void Foam::cachedRandom::reset(const label seed)
|
||||
{
|
||||
seed_ = seed;
|
||||
osRandomSeed(seed_, buffer_);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
Foam::scalar Foam::cachedRandom::sample01()
|
||||
{
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -27,15 +27,6 @@ Class
|
||||
Description
|
||||
Random number generator.
|
||||
|
||||
Pre-computes and caches samples on construction, so that when sample01()
|
||||
is called, the function simply returns the next (pre-computed) sample. On
|
||||
reaching the last sample, the sample sequence is repeated.
|
||||
|
||||
Constructed using a seed and sample count. If the supplied count is
|
||||
negative, no caching is performed, and a new sample is generated on each
|
||||
call to sample01().
|
||||
|
||||
Note: the copy constructor cannot be used if count = -1.
|
||||
|
||||
SourceFiles
|
||||
cachedRandomI.H
|
||||
@ -65,15 +56,12 @@ class cachedRandom
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- Buffer used by re-entrant random number generator
|
||||
List<char> buffer_;
|
||||
|
||||
//- Initial random number seed
|
||||
label seed_;
|
||||
|
||||
//- List of scalar samples
|
||||
scalarList samples_;
|
||||
|
||||
//- Current sample marker
|
||||
label sampleI_;
|
||||
|
||||
//- Indicator, which tells if there is a stored gaussian sample
|
||||
bool hasGaussSample_;
|
||||
|
||||
@ -92,7 +80,7 @@ public:
|
||||
// Constructors
|
||||
|
||||
//- Construct given seed and sample count
|
||||
cachedRandom(const label seed, const label count);
|
||||
cachedRandom(const label seed = 123456);
|
||||
|
||||
//- Copy constructor with optional reset of sampleI
|
||||
cachedRandom(const cachedRandom& cr, const bool reset = false);
|
||||
@ -109,18 +97,7 @@ public:
|
||||
//- Return const access to the initial random number seed
|
||||
inline label seed() const;
|
||||
|
||||
//- Return const access to the list of samples
|
||||
inline const scalarList& samples() const;
|
||||
|
||||
//- Return the current sample marker
|
||||
inline label sampleI() const;
|
||||
|
||||
|
||||
// Manipulation
|
||||
|
||||
//- Return non-const access to the sample marker
|
||||
inline label& sampleI();
|
||||
|
||||
void reset(const label seed);
|
||||
|
||||
// Evaluation
|
||||
|
||||
|
||||
@ -33,22 +33,4 @@ inline Foam::label Foam::cachedRandom::seed() const
|
||||
}
|
||||
|
||||
|
||||
inline const Foam::scalarList& Foam::cachedRandom::samples() const
|
||||
{
|
||||
return samples_;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::cachedRandom::sampleI() const
|
||||
{
|
||||
return sampleI_;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label& Foam::cachedRandom::sampleI()
|
||||
{
|
||||
return sampleI_;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
Reference in New Issue
Block a user