aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-09 19:10:54 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-09 19:10:54 +0000
commit21df391dc6055884e621181cbc3b51d9ec2b095c (patch)
treed92f3423c0585459fdfc0755ae415f0832d478b3 /src
parent21e13f0b2c5242a2ee84b404a96d456e5b753056 (diff)
downloadlibbu++-21df391dc6055884e621181cbc3b51d9ec2b095c.tar.gz
libbu++-21df391dc6055884e621181cbc3b51d9ec2b095c.tar.bz2
libbu++-21df391dc6055884e621181cbc3b51d9ec2b095c.tar.xz
libbu++-21df391dc6055884e621181cbc3b51d9ec2b095c.zip
Added Mersenne Twister random number generator.
Diffstat (limited to '')
-rw-r--r--src/stable/randommersenne.cpp44
-rw-r--r--src/stable/randommersenne.h31
-rw-r--r--src/tests/random.cpp4
3 files changed, 78 insertions, 1 deletions
diff --git a/src/stable/randommersenne.cpp b/src/stable/randommersenne.cpp
new file mode 100644
index 0000000..2d9e85b
--- /dev/null
+++ b/src/stable/randommersenne.cpp
@@ -0,0 +1,44 @@
1#include "bu/randommersenne.h"
2
3Bu::RandomMersenne::RandomMersenne( int32_t iSeed ) :
4 iIndex( 0 )
5{
6 seed( iSeed );
7}
8
9Bu::RandomMersenne::~RandomMersenne()
10{
11}
12
13void Bu::RandomMersenne::seed( int32_t iSeed )
14{
15 iMt[0] = iSeed;
16 for( int j = 1; j < 624; j++ )
17 {
18 iMt[j] = 0x6c078965 * (iMt[j-1]^(iMt[j-1]>>30)) + j;
19 }
20}
21
22int32_t Bu::RandomMersenne::rand()
23{
24 if( iIndex == 0 )
25 {
26 for( int j = 0; j < 624; j++ )
27 {
28 uint32_t y = (iMt[j] & 0x80000000) + (iMt[(j+1)%624] & 0x7fffffff);
29 iMt[j] = iMt[(j+397)%624] ^ (y>>1);
30 if( (y%2) )
31 iMt[j] = iMt[j]^0x9908b0df;
32 }
33 }
34
35 uint32_t y = iMt[iIndex];
36 y = y ^ (y>>11);
37 y = y ^ ((y<<7)&0x9d2c5680);
38 y = y ^ ((y<<15)&0xefc60000);
39 y = y ^ (y>>18);
40
41 iIndex = (iIndex + 1) % 624;
42 return y;
43}
44
diff --git a/src/stable/randommersenne.h b/src/stable/randommersenne.h
new file mode 100644
index 0000000..27c100e
--- /dev/null
+++ b/src/stable/randommersenne.h
@@ -0,0 +1,31 @@
1#ifndef BU_RANDOM_MERSENNE_H
2#define BU_RANDOM_MERSENNE_H
3
4#include "bu/randombase.h"
5
6namespace Bu
7{
8 /**
9 * An implementation of Mersenne Twister (MT19937) algorithm. This as an
10 * algorithm with an excellent reputation for very good random number
11 * generation and a very large period. It is however, relatively slow and
12 * complex compared to, say the Complementary Multiply With Carry
13 * (Bu::RandomCmwc), and may not actually be a *better* random number
14 * generator.
15 */
16 class RandomMersenne : public Bu::RandomBase
17 {
18 public:
19 RandomMersenne( int32_t iSeed=0 );
20 virtual ~RandomMersenne();
21
22 virtual void seed( int32_t iSeed );
23 virtual int32_t rand();
24
25 private:
26 int32_t iIndex;
27 uint32_t iMt[624];
28 };
29};
30
31#endif
diff --git a/src/tests/random.cpp b/src/tests/random.cpp
index 95cdb0a..d7b94ce 100644
--- a/src/tests/random.cpp
+++ b/src/tests/random.cpp
@@ -1,6 +1,7 @@
1#include <bu/randombasic.h> 1#include <bu/randombasic.h>
2#include <bu/randomcmwc.h> 2#include <bu/randomcmwc.h>
3#include <bu/randomsystem.h> 3#include <bu/randomsystem.h>
4#include <bu/randommersenne.h>
4#include <bu/sio.h> 5#include <bu/sio.h>
5#include <time.h> 6#include <time.h>
6 7
@@ -15,7 +16,7 @@ void coverage()
15 uint32_t uBucket[78]; 16 uint32_t uBucket[78];
16 memset( uBucket, 0, sizeof(uint32_t)*78 ); 17 memset( uBucket, 0, sizeof(uint32_t)*78 );
17 18
18 for( int j = 0; j < 1000; j++ ) 19 for( int j = 0; j < 1000000; j++ )
19 { 20 {
20 uBucket[(int)(((uint32_t)rand.rand())/(double)(0xfffffffful)*78+0.5)]++; 21 uBucket[(int)(((uint32_t)rand.rand())/(double)(0xfffffffful)*78+0.5)]++;
21 } 22 }
@@ -43,6 +44,7 @@ int main()
43 coverage<RandomBasic>(); 44 coverage<RandomBasic>();
44 coverage<RandomCmwc>(); 45 coverage<RandomCmwc>();
45 coverage<RandomSystem>(); 46 coverage<RandomSystem>();
47 coverage<RandomMersenne>();
46 48
47 return 0; 49 return 0;
48} 50}