From 79843b51267d7a8faf8e9dcee09fa5e167373e6d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 9 Nov 2012 18:15:04 +0000 Subject: Added seed parameter to constructor of two PRNGs. Also fixed horrible mistake in RandomBasic. --- src/stable/randombasic.cpp | 11 ++++++----- src/stable/randombasic.h | 16 ++++++++++++++-- src/stable/randomcmwc.cpp | 3 ++- src/stable/randomcmwc.h | 2 +- src/tests/random.cpp | 2 +- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/stable/randombasic.cpp b/src/stable/randombasic.cpp index e253b70..fe8843b 100644 --- a/src/stable/randombasic.cpp +++ b/src/stable/randombasic.cpp @@ -7,10 +7,11 @@ #include "bu/randombasic.h" -Bu::RandomBasic::RandomBasic() : - a( 6364136223846793005ll ), - c( 1442695040888963407ll ), - x( 0 ) +#define a (6364136223846793005ll) +#define c (1442695040888963407ll) + +Bu::RandomBasic::RandomBasic( int32_t iSeed ) : + x( iSeed ) { } @@ -20,7 +21,7 @@ Bu::RandomBasic::~RandomBasic() void Bu::RandomBasic::seed( int32_t iSeed ) { - c = iSeed; + x = iSeed; } int32_t Bu::RandomBasic::rand() diff --git a/src/stable/randombasic.h b/src/stable/randombasic.h index 4d1c2f8..c9b7072 100644 --- a/src/stable/randombasic.h +++ b/src/stable/randombasic.h @@ -11,10 +11,22 @@ namespace Bu { + /** + * Basic Linear Congruential Generator. This is one of the simplest and + * most common pseudo-random number generators out there. It's simple, + * fast, and does a decent job if you don't have any stastistical + * requirements other than "random looking." This is the algorithm included + * as the default random number generator in most standard libraries, + * although this one uses larger numbers internally, has a longer period, + * and more even distrubition of randomness accross it's bits than most + * implementations. + * + * The base values were published by Donald Knuth. + */ class RandomBasic : public RandomBase { public: - RandomBasic(); + RandomBasic( int32_t iSeed=0 ); virtual ~RandomBasic(); virtual void seed( int32_t iSeed ); @@ -22,7 +34,7 @@ namespace Bu virtual int32_t rand(); private: - int64_t a, c, x; + int64_t x; }; }; diff --git a/src/stable/randomcmwc.cpp b/src/stable/randomcmwc.cpp index 1d4a913..b1c3cdb 100644 --- a/src/stable/randomcmwc.cpp +++ b/src/stable/randomcmwc.cpp @@ -11,11 +11,12 @@ #define PHI 0x9e3779b9 -Bu::RandomCmwc::RandomCmwc() : +Bu::RandomCmwc::RandomCmwc( int32_t iSeed ) : q( 0 ), c( 362436 ) { q = new uint32_t[4096]; + seed( iSeed ); } Bu::RandomCmwc::~RandomCmwc() diff --git a/src/stable/randomcmwc.h b/src/stable/randomcmwc.h index 547b5bb..50eb8c4 100644 --- a/src/stable/randomcmwc.h +++ b/src/stable/randomcmwc.h @@ -14,7 +14,7 @@ namespace Bu class RandomCmwc : public RandomBase { public: - RandomCmwc(); + RandomCmwc( int32_t iSeed=0 ); virtual ~RandomCmwc(); virtual void seed( int32_t iSeed ); diff --git a/src/tests/random.cpp b/src/tests/random.cpp index e48c543..95cdb0a 100644 --- a/src/tests/random.cpp +++ b/src/tests/random.cpp @@ -15,7 +15,7 @@ void coverage() uint32_t uBucket[78]; memset( uBucket, 0, sizeof(uint32_t)*78 ); - for( int j = 0; j < 1000000; j++ ) + for( int j = 0; j < 1000; j++ ) { uBucket[(int)(((uint32_t)rand.rand())/(double)(0xfffffffful)*78+0.5)]++; } -- cgit v1.2.3