summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-09 18:15:04 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-09 18:15:04 +0000
commit79843b51267d7a8faf8e9dcee09fa5e167373e6d (patch)
treecc317f202fa6b7b68908af9bee1848355681db78
parentba177923d7b60800ae2e01f80fc10dc8ad1594bb (diff)
downloadlibbu++-79843b51267d7a8faf8e9dcee09fa5e167373e6d.tar.gz
libbu++-79843b51267d7a8faf8e9dcee09fa5e167373e6d.tar.bz2
libbu++-79843b51267d7a8faf8e9dcee09fa5e167373e6d.tar.xz
libbu++-79843b51267d7a8faf8e9dcee09fa5e167373e6d.zip
Added seed parameter to constructor of two PRNGs. Also fixed horrible mistake
in RandomBasic.
-rw-r--r--src/stable/randombasic.cpp11
-rw-r--r--src/stable/randombasic.h16
-rw-r--r--src/stable/randomcmwc.cpp3
-rw-r--r--src/stable/randomcmwc.h2
-rw-r--r--src/tests/random.cpp2
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 @@
7 7
8#include "bu/randombasic.h" 8#include "bu/randombasic.h"
9 9
10Bu::RandomBasic::RandomBasic() : 10#define a (6364136223846793005ll)
11 a( 6364136223846793005ll ), 11#define c (1442695040888963407ll)
12 c( 1442695040888963407ll ), 12
13 x( 0 ) 13Bu::RandomBasic::RandomBasic( int32_t iSeed ) :
14 x( iSeed )
14{ 15{
15} 16}
16 17
@@ -20,7 +21,7 @@ Bu::RandomBasic::~RandomBasic()
20 21
21void Bu::RandomBasic::seed( int32_t iSeed ) 22void Bu::RandomBasic::seed( int32_t iSeed )
22{ 23{
23 c = iSeed; 24 x = iSeed;
24} 25}
25 26
26int32_t Bu::RandomBasic::rand() 27int32_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 @@
11 11
12namespace Bu 12namespace Bu
13{ 13{
14 /**
15 * Basic Linear Congruential Generator. This is one of the simplest and
16 * most common pseudo-random number generators out there. It's simple,
17 * fast, and does a decent job if you don't have any stastistical
18 * requirements other than "random looking." This is the algorithm included
19 * as the default random number generator in most standard libraries,
20 * although this one uses larger numbers internally, has a longer period,
21 * and more even distrubition of randomness accross it's bits than most
22 * implementations.
23 *
24 * The base values were published by Donald Knuth.
25 */
14 class RandomBasic : public RandomBase 26 class RandomBasic : public RandomBase
15 { 27 {
16 public: 28 public:
17 RandomBasic(); 29 RandomBasic( int32_t iSeed=0 );
18 virtual ~RandomBasic(); 30 virtual ~RandomBasic();
19 31
20 virtual void seed( int32_t iSeed ); 32 virtual void seed( int32_t iSeed );
@@ -22,7 +34,7 @@ namespace Bu
22 virtual int32_t rand(); 34 virtual int32_t rand();
23 35
24 private: 36 private:
25 int64_t a, c, x; 37 int64_t x;
26 }; 38 };
27}; 39};
28 40
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 @@
11 11
12#define PHI 0x9e3779b9 12#define PHI 0x9e3779b9
13 13
14Bu::RandomCmwc::RandomCmwc() : 14Bu::RandomCmwc::RandomCmwc( int32_t iSeed ) :
15 q( 0 ), 15 q( 0 ),
16 c( 362436 ) 16 c( 362436 )
17{ 17{
18 q = new uint32_t[4096]; 18 q = new uint32_t[4096];
19 seed( iSeed );
19} 20}
20 21
21Bu::RandomCmwc::~RandomCmwc() 22Bu::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
14 class RandomCmwc : public RandomBase 14 class RandomCmwc : public RandomBase
15 { 15 {
16 public: 16 public:
17 RandomCmwc(); 17 RandomCmwc( int32_t iSeed=0 );
18 virtual ~RandomCmwc(); 18 virtual ~RandomCmwc();
19 19
20 virtual void seed( int32_t iSeed ); 20 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()
15 uint32_t uBucket[78]; 15 uint32_t uBucket[78];
16 memset( uBucket, 0, sizeof(uint32_t)*78 ); 16 memset( uBucket, 0, sizeof(uint32_t)*78 );
17 17
18 for( int j = 0; j < 1000000; j++ ) 18 for( int j = 0; j < 1000; j++ )
19 { 19 {
20 uBucket[(int)(((uint32_t)rand.rand())/(double)(0xfffffffful)*78+0.5)]++; 20 uBucket[(int)(((uint32_t)rand.rand())/(double)(0xfffffffful)*78+0.5)]++;
21 } 21 }