From 62e4d2d518b2759ba164947cbe267f2235c26885 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sat, 5 Jul 2003 01:05:47 +0000 Subject: [PATCH] rand gen --- include/ZE_ZRandGen.h | 162 +++++++++++++++++++++++++++++++++++ include/external/mt19937ar.h | 24 ------ 2 files changed, 162 insertions(+), 24 deletions(-) create mode 100755 include/ZE_ZRandGen.h delete mode 100755 include/external/mt19937ar.h diff --git a/include/ZE_ZRandGen.h b/include/ZE_ZRandGen.h new file mode 100755 index 0000000..063c53c --- /dev/null +++ b/include/ZE_ZRandGen.h @@ -0,0 +1,162 @@ +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/*! + \file ZE_ZRandGen.h + \brief Definition file for ZRandGen. + + Definition file for ZRandGen, an OO encapsulated version of the Mersenne Twister. + This class usually isn't needed by the average user, ZEngine contains an instance which + should suffice unless you want objects to have their ownrandom streams. This + implementation is derived from the original mt19937ar.c source from + http://www.math.keio.ac.jp/matumoto/emt.html. See source of ZE_ZRandGen.cpp for license. +
$Id: ZE_ZRandGen.h,v 1.1 2003/07/05 01:05:47 cozman Exp $
+ \author James Turk +**/ + +#ifndef __ze_zrandgen_h__ +#define __ze_zrandgen_h__ + +#include "ZE_Utility.h" +#include + +namespace ZE +{ + +/*! + \brief ZRandGen class for OO encapsulation of superb random generator "Mersenne Twister." + + This class usually isn't needed by the average user, ZEngine contains an instance which + should suffice unless you want objects to have their ownrandom streams. This + implementation is derived from the original mt19937ar.c source from + http://www.math.keio.ac.jp/matumoto/emt.html. See source of ZE_ZRandGen.cpp for license. + \since 0.8.4 +**/ +class ZRandGen +{ + protected: + //! Number of random numbers to generate at once. + static const unsigned long N = 624; + //! A constant used internally by Mersenne Twister. + static const unsigned long M = 397; + //! Constant vector A, used internally by Mersenne Twister. + static const unsigned long MATRIX_A = 0x9908b0dfUL; + //! Most signifigan w-r bits, used internally by Mersenne Twister. + static const unsigned long UPPER_MASK = 0x80000000UL; + //! Least signifigant r bits, used internally by Mersenne Twister. + static const unsigned long LOWER_MASK = 0x7fffffffUL; + + //! Each instance of the algorithm requires a state vector. + unsigned long rStateVector[N]; + //! Used internally to traversing the rStateVector. + int rStateVectorIndex; + + /*! + \brief Workhorse of random generator, only used internally. + + Generates N random numbers and then serves them up, all Rand functions rely + on this. + \return A random unsigned long. + **/ + unsigned long genrand_int32(); + + public: + /*! + \brief Constructor which does an initial seeding using time. + + Creates internal vector and calls Seed(time(NULL)) to initialize the MT state. + **/ + ZRandGen(); + + /*! + \brief Constructor which does an initial seeding. + + Creates internal vector and calls seed to initialize the MT state. + **/ + ZRandGen(unsigned long seed); + + /*! + \brief Seed random number generator. + + Reseed Mersenne Twister random number generator. NOTE: Generator is initialized upon creation of object using time. + \param seed Seed for random sequence. + **/ + void Seed(unsigned long seed); + + /*! + \brief Obtain random integer [0,max). + + Obtain random int l where 0 <= l < max. + \param max Limit for random number. + \return Random unsigned int. + **/ + unsigned int Rand(unsigned int max); + + /*! + \brief Obtain random integer [0,max). + + Obtain random long l where 0 <= l < max. + \param max Limit for random number. + \return Random unsigned long. + **/ + unsigned long Rand(unsigned long max); + + /*! + \brief Obtain random integer [min,max]. + + Obtain random int l where min <= l <= max. + \param min Lower limit for random number. + \param max Upper limit for random number. + \return Random int. + **/ + int Rand(int min, int max); + + /*! + \brief Obtain random integer [min,max]. + + Obtain random long l where min <= l <= max. + \param min Lower limit for random number. + \param max Upper limit for random number. + \return Random long. + **/ + long Rand(long min, long max); + + /*! + \brief Obtain random integer [min,max]. + + Obtain random float l where min <= l <= max. + \param min Lower limit for random number. + \param max Upper limit for random number. + \return Random float. + **/ + float Rand(float min, float max); + + /*! + \brief Obtain random integer [min,max]. + + Obtain random double l where min <= l <= max. + \param min Lower limit for random number. + \param max Upper limit for random number. + \return Random double. + **/ + double Rand(double min, double max); + + /*! + \brief Obtain random double [0,1). + + Obtain random double d where 0 <= d < 1. + \return Random double [0,1). + **/ + double RandDouble(); +}; + +} + +#endif //__ze_zrandgen_h__ \ No newline at end of file diff --git a/include/external/mt19937ar.h b/include/external/mt19937ar.h deleted file mode 100755 index 656d654..0000000 --- a/include/external/mt19937ar.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __mt19937ar__ -#define __mt19937ar__ - -//This .h file written for ZEngine by James Turk to declare the methods implemented in mt19937ar.c -//See mt19937ar.c for license of the Mersenne Twister. - -#include //changed for ZEngine -/* Period parameters */ -#define N 624 -#define M 397 -#define MATRIX_A 0x9908b0dfUL /* constant vector a */ -#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ -#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ - -void init_genrand(unsigned long s); -unsigned long genrand_int32(void); -long genrand_int31(void); -double genrand_real1(void); -double genrand_real2(void); -double genrand_real3(void); -double genrand_res53(void); - -#endif //__mt19937ar__ -