From 4262c7fd96f54be4530c6ec18e1154faaa9c31af Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 30 Jul 2003 20:31:50 +0000 Subject: [PATCH] Add an SDL random number generator with independent seeds. --- stepmania/src/RageUtil.cpp | 14 ++++++++++++++ stepmania/src/RageUtil.h | 13 +++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 9e2165c512..704c56bbe2 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -25,6 +25,20 @@ unsigned long randseed = time(NULL); +RandomGen::RandomGen( unsigned long seed_ ) +{ + seed = seed_; + if(seed == 0) + seed = time(NULL); +} + +int RandomGen::operator() ( int maximum ) +{ + seed = 1664525L * seed + 1013904223L; + return (seed >> 2) % (maximum + 1); +} + + void fapproach( float& val, float other_val, float to_move ) { ASSERT( to_move >= 0 ); diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index e446220302..84a51d74ce 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -92,10 +92,15 @@ inline int RandomInt(int nLow, int nHigh) return ((Random() >> 2) % (nHigh - nLow + 1)) + nLow; } -// Debug new for memory leak tracing -//#ifdef _DEBUG -//#define new new(_NORMAL_BLOCK, __FILE__, __LINE__) -//#endif +/* Alternative: */ +class RandomGen +{ + unsigned long seed; + +public: + RandomGen( unsigned long seed = 0 ); + int operator() ( int maximum = INT_MAX-1 ); +}; // Simple function for generating random numbers