diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 16:57:15 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 16:57:15 +0000 |
commit | 2295579eb790d6eff6e54e84c01da6de10809a71 (patch) | |
tree | 3d8282dedc137fee009f71ff904370537009e504 /src/experimental | |
parent | 690ad7280e655654a3bcca2ca5ced9caacf75c8b (diff) | |
download | libbu++-2295579eb790d6eff6e54e84c01da6de10809a71.tar.gz libbu++-2295579eb790d6eff6e54e84c01da6de10809a71.tar.bz2 libbu++-2295579eb790d6eff6e54e84c01da6de10809a71.tar.xz libbu++-2295579eb790d6eff6e54e84c01da6de10809a71.zip |
Better win_o ignores. The random number system is pretty much together.
We need a few extra helper functions to cover some other good things, like
normalized floating point numbers, etc.
Diffstat (limited to 'src/experimental')
-rw-r--r-- | src/experimental/random.cpp | 24 | ||||
-rw-r--r-- | src/experimental/random.h | 51 | ||||
-rw-r--r-- | src/experimental/randombase.cpp | 8 | ||||
-rw-r--r-- | src/experimental/randombase.h | 17 | ||||
-rw-r--r-- | src/experimental/randombasic.h | 8 | ||||
-rw-r--r-- | src/experimental/randomcmwc.h | 8 | ||||
-rw-r--r-- | src/experimental/randomsystem.cpp | 40 | ||||
-rw-r--r-- | src/experimental/randomsystem.h | 37 |
8 files changed, 185 insertions, 8 deletions
diff --git a/src/experimental/random.cpp b/src/experimental/random.cpp index c792772..725948a 100644 --- a/src/experimental/random.cpp +++ b/src/experimental/random.cpp | |||
@@ -4,5 +4,29 @@ | |||
4 | * This file is part of the libbu++ library and is released under the | 4 | * This file is part of the libbu++ library and is released under the |
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | #include "bu/random.h" | ||
7 | 8 | ||
9 | #include "bu/randombasic.h" | ||
10 | |||
11 | Bu::Random::Random() : | ||
12 | pGen( NULL ) | ||
13 | { | ||
14 | pGen = new RandomBasic(); | ||
15 | } | ||
16 | |||
17 | Bu::Random::~Random() | ||
18 | { | ||
19 | delete pGen; | ||
20 | pGen = NULL; | ||
21 | } | ||
22 | |||
23 | int32_t Bu::Random::rand() | ||
24 | { | ||
25 | return getInstance().pGen->rand(); | ||
26 | } | ||
27 | |||
28 | void Bu::Random::seed( int32_t iSeed ) | ||
29 | { | ||
30 | getInstance().pGen->seed( iSeed ); | ||
31 | } | ||
8 | 32 | ||
diff --git a/src/experimental/random.h b/src/experimental/random.h index c792772..ba26f21 100644 --- a/src/experimental/random.h +++ b/src/experimental/random.h | |||
@@ -4,5 +4,56 @@ | |||
4 | * This file is part of the libbu++ library and is released under the | 4 | * This file is part of the libbu++ library and is released under the |
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | #ifndef BU_RANDOM_H | ||
8 | #define BU_RANDOM_H | ||
7 | 9 | ||
10 | #include "bu/singleton.h" | ||
11 | #include <stdint.h> | ||
12 | |||
13 | namespace Bu | ||
14 | { | ||
15 | class RandomBase; | ||
16 | class Random : public Bu::Singleton<Bu::Random> | ||
17 | { | ||
18 | friend class Bu::Singleton<Bu::Random>; | ||
19 | private: | ||
20 | Random(); | ||
21 | virtual ~Random(); | ||
22 | |||
23 | public: | ||
24 | template<typename cl> | ||
25 | static void setGenerator() | ||
26 | { | ||
27 | delete getInstance().pGen; | ||
28 | getInstance().pGen = new cl(); | ||
29 | } | ||
30 | |||
31 | template<typename cl, typename t1> | ||
32 | static void setGenerator( t1 p1 ) | ||
33 | { | ||
34 | delete getInstance().pGen; | ||
35 | getInstance().pGen = new cl( p1 ); | ||
36 | } | ||
37 | |||
38 | template<typename cl, typename t1, typename t2> | ||
39 | static void setGenerator( t1 p1, t2 p2 ) | ||
40 | { | ||
41 | delete getInstance().pGen; | ||
42 | getInstance().pGen = new cl( p1, p2 ); | ||
43 | } | ||
44 | |||
45 | RandomBase &getGenerator() { return *pGen; } | ||
46 | |||
47 | static int32_t rand(); | ||
48 | static void seed( int32_t iSeed ); | ||
49 | |||
50 | private: | ||
51 | void checkInit(); | ||
52 | |||
53 | private: | ||
54 | Bu::RandomBase *pGen; | ||
55 | }; | ||
56 | }; | ||
57 | |||
58 | #endif | ||
8 | 59 | ||
diff --git a/src/experimental/randombase.cpp b/src/experimental/randombase.cpp index c792772..6514df3 100644 --- a/src/experimental/randombase.cpp +++ b/src/experimental/randombase.cpp | |||
@@ -4,5 +4,13 @@ | |||
4 | * This file is part of the libbu++ library and is released under the | 4 | * This file is part of the libbu++ library and is released under the |
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | #include "bu/randombase.h" | ||
7 | 8 | ||
9 | Bu::RandomBase::RandomBase() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Bu::RandomBase::~RandomBase() | ||
14 | { | ||
15 | } | ||
8 | 16 | ||
diff --git a/src/experimental/randombase.h b/src/experimental/randombase.h index c792772..aff1d45 100644 --- a/src/experimental/randombase.h +++ b/src/experimental/randombase.h | |||
@@ -4,5 +4,22 @@ | |||
4 | * This file is part of the libbu++ library and is released under the | 4 | * This file is part of the libbu++ library and is released under the |
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | #ifndef BU_RANDOM_BASE_H | ||
8 | #define BU_RANDOM_BASE_H | ||
7 | 9 | ||
10 | #include <stdint.h> | ||
8 | 11 | ||
12 | namespace Bu | ||
13 | { | ||
14 | class RandomBase | ||
15 | { | ||
16 | public: | ||
17 | RandomBase(); | ||
18 | virtual ~RandomBase(); | ||
19 | |||
20 | virtual void seed( int32_t iSeed )=0; | ||
21 | virtual int32_t rand()=0; | ||
22 | }; | ||
23 | }; | ||
24 | |||
25 | #endif | ||
diff --git a/src/experimental/randombasic.h b/src/experimental/randombasic.h index 6eab9dc..a53e16f 100644 --- a/src/experimental/randombasic.h +++ b/src/experimental/randombasic.h | |||
@@ -7,19 +7,19 @@ | |||
7 | #ifndef BU_RANDOM_BASIC_H | 7 | #ifndef BU_RANDOM_BASIC_H |
8 | #define BU_RANDOM_BASIC_H | 8 | #define BU_RANDOM_BASIC_H |
9 | 9 | ||
10 | #include <stdint.h> | 10 | #include "bu/randombase.h" |
11 | 11 | ||
12 | namespace Bu | 12 | namespace Bu |
13 | { | 13 | { |
14 | class RandomBasic | 14 | class RandomBasic : public RandomBase |
15 | { | 15 | { |
16 | public: | 16 | public: |
17 | RandomBasic(); | 17 | RandomBasic(); |
18 | virtual ~RandomBasic(); | 18 | virtual ~RandomBasic(); |
19 | 19 | ||
20 | void seed( int32_t iSeed ); | 20 | virtual void seed( int32_t iSeed ); |
21 | 21 | ||
22 | int32_t rand(); | 22 | virtual int32_t rand(); |
23 | 23 | ||
24 | private: | 24 | private: |
25 | int64_t a, c, x; | 25 | int64_t a, c, x; |
diff --git a/src/experimental/randomcmwc.h b/src/experimental/randomcmwc.h index 3a05383..747eb6a 100644 --- a/src/experimental/randomcmwc.h +++ b/src/experimental/randomcmwc.h | |||
@@ -7,19 +7,19 @@ | |||
7 | #ifndef BU_RANDOM_CMWC_H | 7 | #ifndef BU_RANDOM_CMWC_H |
8 | #define BU_RANDOM_CMWC_H | 8 | #define BU_RANDOM_CMWC_H |
9 | 9 | ||
10 | #include <stdint.h> | 10 | #include "bu/randombase.h" |
11 | 11 | ||
12 | namespace Bu | 12 | namespace Bu |
13 | { | 13 | { |
14 | class RandomCmwc | 14 | class RandomCmwc : public RandomBase |
15 | { | 15 | { |
16 | public: | 16 | public: |
17 | RandomCmwc(); | 17 | RandomCmwc(); |
18 | virtual ~RandomCmwc(); | 18 | virtual ~RandomCmwc(); |
19 | 19 | ||
20 | void seed( int32_t iSeed ); | 20 | virtual void seed( int32_t iSeed ); |
21 | 21 | ||
22 | int32_t rand(); | 22 | virtual int32_t rand(); |
23 | 23 | ||
24 | private: | 24 | private: |
25 | uint32_t *q, c; | 25 | uint32_t *q, c; |
diff --git a/src/experimental/randomsystem.cpp b/src/experimental/randomsystem.cpp new file mode 100644 index 0000000..0501587 --- /dev/null +++ b/src/experimental/randomsystem.cpp | |||
@@ -0,0 +1,40 @@ | |||
1 | #include "bu/randomsystem.h" | ||
2 | #include "bu/file.h" | ||
3 | |||
4 | Bu::RandomSystem::RandomSystem( Type eType ) : | ||
5 | eType( eType ), | ||
6 | pSrc( 0 ) | ||
7 | { | ||
8 | switch( eType ) | ||
9 | { | ||
10 | case Bu::RandomSystem::Fast: | ||
11 | pSrc = new Bu::File("/dev/urandom", Bu::File::Read ); | ||
12 | break; | ||
13 | |||
14 | case Bu::RandomSystem::Good: | ||
15 | pSrc = new Bu::File("/dev/random", Bu::File::Read ); | ||
16 | break; | ||
17 | } | ||
18 | } | ||
19 | |||
20 | Bu::RandomSystem::~RandomSystem() | ||
21 | { | ||
22 | delete pSrc; | ||
23 | } | ||
24 | |||
25 | void Bu::RandomSystem::seed( int32_t /*iSeed*/ ) | ||
26 | { | ||
27 | // Seed really has no effect here... | ||
28 | // on linux, if we were root, we could write data to random/urandom to | ||
29 | // perturb the data, but it's not necesarry | ||
30 | } | ||
31 | |||
32 | int32_t Bu::RandomSystem::rand() | ||
33 | { | ||
34 | if( !pSrc ) | ||
35 | throw Bu::ExceptionBase("Not initialized"); | ||
36 | int32_t i; | ||
37 | pSrc->read( &i, sizeof(int32_t) ); | ||
38 | return i; | ||
39 | } | ||
40 | |||
diff --git a/src/experimental/randomsystem.h b/src/experimental/randomsystem.h new file mode 100644 index 0000000..7106d58 --- /dev/null +++ b/src/experimental/randomsystem.h | |||
@@ -0,0 +1,37 @@ | |||
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_SYSTEM_H | ||
8 | #define BU_RANDOM_SYSTEM_H | ||
9 | |||
10 | #include "bu/randombase.h" | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class File; | ||
15 | class RandomSystem : public RandomBase | ||
16 | { | ||
17 | public: | ||
18 | enum Type | ||
19 | { | ||
20 | Fast, | ||
21 | Good | ||
22 | }; | ||
23 | |||
24 | RandomSystem( Type eType=Fast ); | ||
25 | virtual ~RandomSystem(); | ||
26 | |||
27 | virtual void seed( int32_t iSeed ); | ||
28 | |||
29 | virtual int32_t rand(); | ||
30 | |||
31 | private: | ||
32 | Type eType; | ||
33 | File *pSrc; | ||
34 | }; | ||
35 | }; | ||
36 | |||
37 | #endif | ||