From 20ee81a7b2d44ec985e99b4ee1bcd08fa25175d3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 23 May 2012 00:39:31 +0000 Subject: We have a nice selection of basic randomness functions now. --- src/stable/file.cpp | 1 - src/stable/random.cpp | 19 +++++++++++++++++-- src/stable/random.h | 5 ++++- src/stable/randombase.cpp | 15 +++++++++++++++ src/stable/randombase.h | 3 +++ 5 files changed, 39 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/stable/file.cpp b/src/stable/file.cpp index 81b476d..33420f1 100644 --- a/src/stable/file.cpp +++ b/src/stable/file.cpp @@ -190,7 +190,6 @@ void Bu::File::setBlocking( bool bBlocking ) Bu::File Bu::File::tempFile( Bu::String &sName ) { - uint32_t iX; int iXes; for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) { diff --git a/src/stable/random.cpp b/src/stable/random.cpp index 725948a..63416bf 100644 --- a/src/stable/random.cpp +++ b/src/stable/random.cpp @@ -20,13 +20,28 @@ Bu::Random::~Random() pGen = NULL; } +void Bu::Random::seed( int32_t iSeed ) +{ + getInstance().pGen->seed( iSeed ); +} + int32_t Bu::Random::rand() { return getInstance().pGen->rand(); } -void Bu::Random::seed( int32_t iSeed ) +int32_t Bu::Random::rand( int32_t iMax ) { - getInstance().pGen->seed( iSeed ); + return getInstance().pGen->rand( iMax ); +} + +int32_t Bu::Random::rand( int32_t iMin, int32_t iMax ) +{ + return getInstance().pGen->rand( iMin, iMax ); +} + +double Bu::Random::randNorm() +{ + return getInstance().pGen->randNorm(); } diff --git a/src/stable/random.h b/src/stable/random.h index ba26f21..43b8917 100644 --- a/src/stable/random.h +++ b/src/stable/random.h @@ -44,8 +44,11 @@ namespace Bu RandomBase &getGenerator() { return *pGen; } - static int32_t rand(); static void seed( int32_t iSeed ); + static int32_t rand(); + static int32_t rand( int32_t iMax ); + static int32_t rand( int32_t iMin, int32_t iMax ); + static double randNorm(); private: void checkInit(); diff --git a/src/stable/randombase.cpp b/src/stable/randombase.cpp index 6514df3..71a5c89 100644 --- a/src/stable/randombase.cpp +++ b/src/stable/randombase.cpp @@ -14,3 +14,18 @@ Bu::RandomBase::~RandomBase() { } +int32_t Bu::RandomBase::rand( int32_t iMax ) +{ + return rand( 0, iMax ); +} + +int32_t Bu::RandomBase::rand( int32_t iMin, int32_t iMax ) +{ + return iMin+(randNorm()*(iMax-iMin)); +} + +double Bu::RandomBase::randNorm() +{ + return (((uint32_t)rand())&0xfffffffeul)/(double)(0xfffffffful); +} + diff --git a/src/stable/randombase.h b/src/stable/randombase.h index aff1d45..a5a4f42 100644 --- a/src/stable/randombase.h +++ b/src/stable/randombase.h @@ -19,6 +19,9 @@ namespace Bu virtual void seed( int32_t iSeed )=0; virtual int32_t rand()=0; + virtual int32_t rand( int32_t iMax ); + virtual int32_t rand( int32_t iMin, int32_t iMax ); + virtual double randNorm(); }; }; -- cgit v1.2.3