diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-05-04 18:44:53 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-05-04 18:44:53 +0000 |
commit | 55431ec82f1db436938125d9d6169aab79cbd3d3 (patch) | |
tree | af9c31eff2a44efacded45dca5bcb027ffbe743d /src/experimental | |
parent | 3e3f79bc35d94fab81cb31e42d8699a7eee102a4 (diff) | |
download | libbu++-55431ec82f1db436938125d9d6169aab79cbd3d3.tar.gz libbu++-55431ec82f1db436938125d9d6169aab79cbd3d3.tar.bz2 libbu++-55431ec82f1db436938125d9d6169aab79cbd3d3.tar.xz libbu++-55431ec82f1db436938125d9d6169aab79cbd3d3.zip |
Two basic random number generators, Cmwc is supposed to be a pretty good one.
I need to get the base class and singleton interface in place.
Diffstat (limited to '')
-rw-r--r-- | src/experimental/randombasic.cpp | 20 | ||||
-rw-r--r-- | src/experimental/randombasic.h | 6 | ||||
-rw-r--r-- | src/experimental/randomcmwc.cpp | 52 | ||||
-rw-r--r-- | src/experimental/randomcmwc.h | 29 |
4 files changed, 104 insertions, 3 deletions
diff --git a/src/experimental/randombasic.cpp b/src/experimental/randombasic.cpp index 03bf7fe..ac591be 100644 --- a/src/experimental/randombasic.cpp +++ b/src/experimental/randombasic.cpp | |||
@@ -8,8 +8,24 @@ | |||
8 | #include "bu/randombasic.h" | 8 | #include "bu/randombasic.h" |
9 | 9 | ||
10 | Bu::RandomBasic::RandomBasic() : | 10 | Bu::RandomBasic::RandomBasic() : |
11 | c( 6364136223846793005 ), | 11 | a( 6364136223846793005 ), |
12 | m( 1442695040888963407 ) | 12 | c( 1442695040888963407 ), |
13 | x( 0 ) | ||
13 | { | 14 | { |
14 | } | 15 | } |
15 | 16 | ||
17 | Bu::RandomBasic::~RandomBasic() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | void Bu::RandomBasic::seed( int32_t iSeed ) | ||
22 | { | ||
23 | c = iSeed; | ||
24 | } | ||
25 | |||
26 | int32_t Bu::RandomBasic::rand() | ||
27 | { | ||
28 | x = (a*x + c); | ||
29 | return (int32_t)x; | ||
30 | } | ||
31 | |||
diff --git a/src/experimental/randombasic.h b/src/experimental/randombasic.h index 2ba7bd3..6eab9dc 100644 --- a/src/experimental/randombasic.h +++ b/src/experimental/randombasic.h | |||
@@ -17,8 +17,12 @@ namespace Bu | |||
17 | RandomBasic(); | 17 | RandomBasic(); |
18 | virtual ~RandomBasic(); | 18 | virtual ~RandomBasic(); |
19 | 19 | ||
20 | void seed( int32_t iSeed ); | ||
21 | |||
22 | int32_t rand(); | ||
23 | |||
20 | private: | 24 | private: |
21 | int64_t c, m, a; | 25 | int64_t a, c, x; |
22 | }; | 26 | }; |
23 | }; | 27 | }; |
24 | 28 | ||
diff --git a/src/experimental/randomcmwc.cpp b/src/experimental/randomcmwc.cpp new file mode 100644 index 0000000..97f7262 --- /dev/null +++ b/src/experimental/randomcmwc.cpp | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/randomcmwc.h" | ||
9 | |||
10 | #define PHI 0x9e3779b9 | ||
11 | |||
12 | RandomCmwc::RandomCmwc() : | ||
13 | q( NULL ), | ||
14 | c( 362436 ) | ||
15 | { | ||
16 | q = new uint32_t[4096]; | ||
17 | } | ||
18 | |||
19 | RandomCmwc::~RandomCmwc() | ||
20 | { | ||
21 | delete[] q; | ||
22 | } | ||
23 | |||
24 | void RandomCmwc::seed( int32_t iSeed ) | ||
25 | { | ||
26 | int i; | ||
27 | |||
28 | Q[0] = iSeed; | ||
29 | Q[1] = iSeed + PHI; | ||
30 | Q[2] = iSeed + PHI + PHI; | ||
31 | |||
32 | for (i = 3; i < 4096; i++) | ||
33 | Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; | ||
34 | } | ||
35 | |||
36 | int32_t RandomCmwc::rand() | ||
37 | { | ||
38 | uint64_t t, a = 18782LL; | ||
39 | static uint32_t i = 4095; | ||
40 | uint32_t x, r = 0xfffffffe; | ||
41 | i = (i + 1) & 4095; | ||
42 | t = a * Q[i] + c; | ||
43 | c = (t >> 32); | ||
44 | x = t + c; | ||
45 | if( x < c ) | ||
46 | { | ||
47 | x++; | ||
48 | c++; | ||
49 | } | ||
50 | return (Q[i] = r - x); | ||
51 | } | ||
52 | |||
diff --git a/src/experimental/randomcmwc.h b/src/experimental/randomcmwc.h new file mode 100644 index 0000000..3a05383 --- /dev/null +++ b/src/experimental/randomcmwc.h | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_CMWC_H | ||
8 | #define BU_RANDOM_CMWC_H | ||
9 | |||
10 | #include <stdint.h> | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class RandomCmwc | ||
15 | { | ||
16 | public: | ||
17 | RandomCmwc(); | ||
18 | virtual ~RandomCmwc(); | ||
19 | |||
20 | void seed( int32_t iSeed ); | ||
21 | |||
22 | int32_t rand(); | ||
23 | |||
24 | private: | ||
25 | uint32_t *q, c; | ||
26 | }; | ||
27 | }; | ||
28 | |||
29 | #endif | ||