From d70351d50a6703604843c10f8cc36715f65cef5e Mon Sep 17 00:00:00 2001 From: langmead Date: Fri, 11 Dec 2009 19:36:11 +0000 Subject: [PATCH] *** empty log message *** --- random_source.h | 4 +++- random_test.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 random_test.cpp diff --git a/random_source.h b/random_source.h index ac4bcc3..65ec646 100644 --- a/random_source.h +++ b/random_source.h @@ -1,6 +1,8 @@ #ifndef RANDOM_GEN_H_ #define RANDOM_GEN_H_ +#include "assert_helpers.h" + /** * Simple pseudo-random linear congruential generator, a la Numerical * Recipes. @@ -26,7 +28,7 @@ class RandomSource { last = a * last + c; ret = last >> 16; last = a * last + c; - ret |= last; + ret ^= last; lastOff = 0; return ret; } diff --git a/random_test.cpp b/random_test.cpp new file mode 100644 index 0000000..f8c21a6 --- /dev/null +++ b/random_test.cpp @@ -0,0 +1,48 @@ +/* + * random_test.cpp + * + * Created on: Dec 11, 2009 + * Author: Ben Langmead + */ + +#include +#include +#include "random_source.h" + +using namespace std; + +int main(void) { + RandomSource rand; + rand.init(0); + uint32_t ts[32]; + memset(ts, 0, 32*sizeof(uint32_t)); + uint32_t r = rand.nextU32(); + cout << "Without reseeding:" << endl; + for(int i = 0; i < 10000; i++) { + uint32_t nr = rand.nextU32(); + for(int j = 0; j < 32; j++) { + if(((r >> j) & 1) != ((nr >> j) & 1)) { + ts[j]++; + } + } + } + for(int j = 0; j < 32; j++) { + cout << ts[j] << endl; + } + memset(ts, 0, 32*sizeof(uint32_t)); + rand.init(0); + r = rand.nextU32(); + cout << "With reseeding:" << endl; + for(int i = 0; i < 10000; i++) { + rand.init(i+1); + uint32_t nr = rand.nextU32(); + for(int j = 0; j < 32; j++) { + if(((r >> j) & 1) != ((nr >> j) & 1)) { + ts[j]++; + } + } + } + for(int j = 0; j < 32; j++) { + cout << ts[j] << endl; + } +}