From 55431ec82f1db436938125d9d6169aab79cbd3d3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 4 May 2012 18:44:53 +0000 Subject: 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. --- src/experimental/randombasic.cpp | 20 ++++++++++++++-- src/experimental/randombasic.h | 6 ++++- src/experimental/randomcmwc.cpp | 52 ++++++++++++++++++++++++++++++++++++++++ src/experimental/randomcmwc.h | 29 ++++++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/experimental/randomcmwc.cpp create mode 100644 src/experimental/randomcmwc.h (limited to 'src') 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 @@ #include "bu/randombasic.h" Bu::RandomBasic::RandomBasic() : - c( 6364136223846793005 ), - m( 1442695040888963407 ) + a( 6364136223846793005 ), + c( 1442695040888963407 ), + x( 0 ) { } +Bu::RandomBasic::~RandomBasic() +{ +} + +void Bu::RandomBasic::seed( int32_t iSeed ) +{ + c = iSeed; +} + +int32_t Bu::RandomBasic::rand() +{ + x = (a*x + c); + return (int32_t)x; +} + 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 RandomBasic(); virtual ~RandomBasic(); + void seed( int32_t iSeed ); + + int32_t rand(); + private: - int64_t c, m, a; + int64_t a, c, x; }; }; 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 @@ +/* + * Copyright (C) 2007-2012 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/randomcmwc.h" + +#define PHI 0x9e3779b9 + +RandomCmwc::RandomCmwc() : + q( NULL ), + c( 362436 ) +{ + q = new uint32_t[4096]; +} + +RandomCmwc::~RandomCmwc() +{ + delete[] q; +} + +void RandomCmwc::seed( int32_t iSeed ) +{ + int i; + + Q[0] = iSeed; + Q[1] = iSeed + PHI; + Q[2] = iSeed + PHI + PHI; + + for (i = 3; i < 4096; i++) + Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; +} + +int32_t RandomCmwc::rand() +{ + uint64_t t, a = 18782LL; + static uint32_t i = 4095; + uint32_t x, r = 0xfffffffe; + i = (i + 1) & 4095; + t = a * Q[i] + c; + c = (t >> 32); + x = t + c; + if( x < c ) + { + x++; + c++; + } + return (Q[i] = r - x); +} + 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 @@ +/* + * Copyright (C) 2007-2012 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ +#ifndef BU_RANDOM_CMWC_H +#define BU_RANDOM_CMWC_H + +#include + +namespace Bu +{ + class RandomCmwc + { + public: + RandomCmwc(); + virtual ~RandomCmwc(); + + void seed( int32_t iSeed ); + + int32_t rand(); + + private: + uint32_t *q, c; + }; +}; + +#endif -- cgit v1.2.3