From 2a89555d0a79f9335147650aeba27b123f809ca2 Mon Sep 17 00:00:00 2001 From: mattijs <mattijs> Date: Fri, 1 Jul 2011 16:14:10 +0100 Subject: [PATCH] COMP: Random: moved to OSspecific --- src/OSspecific/POSIX/POSIX.C | 38 ++++++++++++++++++- src/OpenFOAM/include/OSspecific.H | 12 ++++++ .../primitives/random/Random/Random.C | 31 +++------------ .../random/cachedRandom/cachedRandom.C | 8 ++-- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 48e4b3a4dfe..dba1e73f3a5 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -57,6 +57,10 @@ Description #include <netinet/in.h> +#ifdef USE_RANDOM +# include <climits> +#endif + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // defineTypeNameAndDebug(Foam::POSIX, 0); @@ -68,16 +72,19 @@ pid_t Foam::pid() return ::getpid(); } + pid_t Foam::ppid() { return ::getppid(); } + pid_t Foam::pgid() { return ::getpgrp(); } + bool Foam::env(const word& envName) { return ::getenv(envName.c_str()) != NULL; @@ -890,7 +897,6 @@ bool Foam::mvBak(const fileName& src, const std::string& ext) } - // Remove a file, returning true if successful otherwise false bool Foam::rm(const fileName& file) { @@ -1221,4 +1227,34 @@ Foam::fileNameList Foam::dlLoaded() } +void Foam::osRandomSeed(const label seed) +{ +#ifdef USE_RANDOM + srandom((unsigned int)seed); +#else + srand48(seed); +#endif +} + + +Foam::label Foam::osRandomInteger() +{ +#ifdef USE_RANDOM + return random(); +#else + return lrand48(); +#endif +} + + +Foam::scalar Foam::osRandomDouble() +{ +#ifdef USE_RANDOM + return (scalar)random(); +#else + return drand48(); +#endif +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index f01ad296476..b7749973397 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -200,6 +200,18 @@ bool dlSymFound(void* handle, const std::string& symbol); fileNameList dlLoaded(); +// Low level random numbers. Use Random class instead. + +//- Seed random number generator. +void osRandomSeed(const label seed); + +//- Return random integer (uniform distribution between 0 and 2^31) +label osRandomInteger(); + +//- Return random double precision (uniform distribution between 0 and 1) +scalar osRandomDouble(); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/primitives/random/Random/Random.C b/src/OpenFOAM/primitives/random/Random/Random.C index 704e5ef1181..923a656bb96 100644 --- a/src/OpenFOAM/primitives/random/Random/Random.C +++ b/src/OpenFOAM/primitives/random/Random/Random.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "Random.H" +#include "OSspecific.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -37,11 +38,6 @@ namespace Foam # error "The random number generator may not work!" #endif -#ifdef USE_RANDOM -# include <climits> -#else -# include <cstdlib> -#endif // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -57,22 +53,13 @@ Random::Random(const label seed) Seed = 1; } -# ifdef USE_RANDOM - srandom((unsigned int)Seed); -# else - srand48(Seed); -# endif - + osRandomSeed(Seed); } int Random::bit() { -# ifdef USE_RANDOM - if (random() > INT_MAX/2) -# else - if (lrand48() > INT_MAX/2) -# endif + if (osRandomInteger() > INT_MAX/2) { return 1; } @@ -85,11 +72,7 @@ int Random::bit() scalar Random::scalar01() { -# ifdef USE_RANDOM - return (scalar)random()/INT_MAX; -# else - return drand48(); -# endif + return osRandomDouble(); } @@ -140,11 +123,7 @@ tensor Random::tensor01() label Random::integer(const label lower, const label upper) { -# ifdef USE_RANDOM - return lower + (random() % (upper+1-lower)); -# else - return lower + (lrand48() % (upper+1-lower)); -# endif + return lower + (osRandomInteger() % (upper+1-lower)); } diff --git a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C index b48c1f4e632..6db63990f5a 100644 --- a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C +++ b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C @@ -24,7 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "cachedRandom.H" -#include <cstdlib> +#include "OSspecific.H" #if INT_MAX != 2147483647 # error "INT_MAX != 2147483647" @@ -37,7 +37,7 @@ Foam::scalar Foam::cachedRandom::scalar01() { if (sampleI_ < 0) { - return drand48(); + return osRandomDouble(); } if (sampleI_ == samples_.size() - 1) @@ -76,7 +76,7 @@ Foam::cachedRandom::cachedRandom(const label seed, const label count) } // Initialise samples - srand48(seed_); + osRandomSeed(seed_); forAll(samples_, i) { samples_[i] = drand48(); @@ -98,7 +98,7 @@ Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset) ) << "Copy constructor called, but samples not being cached. " << "This may lead to non-repeatable behaviour" << endl; - srand48(seed_); + osRandomSeed(seed_); } else if (reset) { -- GitLab