diff --git a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C index e2fec45161..47dd8e1c9e 100644 --- a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C +++ b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C @@ -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() { diff --git a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.H b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.H index a42034284a..9e4224805c 100644 --- a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.H +++ b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.H @@ -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 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 diff --git a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandomI.H b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandomI.H index 8d23cbcede..e42e72329b 100644 --- a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandomI.H +++ b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandomI.H @@ -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_; -} - - // ************************************************************************* //