randgenerator.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: RandGenerator.h
00004 
00005     DESCRIPTION: Generator of random numbers
00006 
00007     CREATED BY: Oleg Bayborodin
00008 
00009     HISTORY: Nov.17 2000 - created
00010 
00011     GOALS: To solve the need in satisfactory generator of random numbers.
00012 
00013     DESCRIPTION: The class has interfaces for srand/rand methods of VC++ 
00014         and other functions for random number generation.
00015 
00016         Srand/rand methods from stdlib.h have two main problems:
00017         a) It's not satisfactory random. Since rand() function returns a 
00018             pseudorandom integer in the range 0 to 0x7fff=32767, if we
00019             need a lot of random numbers (i.e. for generating 100,000 
00020             particles), we are running out of continuity of random num-
00021             bers. Generated random numbers became too discrete.
00022         b) rand() method is global function, not class object. Hence it's
00023             shared between all modules of your plug-in. Changes in one
00024             module may change randomness pattern in other independent 
00025             module. To solve this contradiction, rand methods have to be
00026             implemented as a class object.
00027 
00028         RandGenerator does exactly that. It has much more random numbers:
00029         RAND_MAX = 0xFFFFFFFF = 4,294,967,295. Also, using instance of 
00030         the class, it's much easier to create separate thread of random 
00031         numbers for specific module.
00032 
00033  *> Copyright (c) 2000, All Rights Reserved.
00034  **********************************************************************/
00035 
00036 #pragma once
00037 #include <WTypes.h>
00038 #include "coreexp.h"
00039 #include "maxheap.h"
00040 
00041 
00042 // to fix conflict between RandGenerator and stdlib.h do the following:
00043 #undef RAND_MAX
00044 const unsigned int RAND_MAX = 0x7fff; // as defined in stdlib.h
00045 
00069 class RandGenerator: public MaxHeapOperators 
00070 {
00071     public:
00073         CoreExport RandGenerator(); // constructor
00074     
00075     // override for VC++ rand methods
00076         CoreExport static const DWORD32 RAND_MAX;
00077 
00078         // The srand function sets the starting point for generating a 
00079         // series of pseudorandom integers. To reinitialize the generator, 
00080         // use 1 as the seed argument. Any other value for seed sets the 
00081         // generator to a random starting point. rand() retrieves the 
00082         // pseudorandom numbers that are generated. Calling rand() before 
00083         // any call to srand() generates the same sequence as calling srand() 
00084         // with seed passed as 1.
00094         CoreExport void srand(DWORD32 seed);
00095         // The rand function returns a pseudorandom integer in the range 0 to RAND_MAX
00097         CoreExport DWORD32 rand( void );
00098 
00099     // other useful functions:
00101         CoreExport int      RandSign( void );
00103         CoreExport float    Rand01( void );
00105         CoreExport float    Rand11( void );
00107         CoreExport float    Rand55( void );
00109         CoreExport int      Rand0X(int maxnum);
00110 
00113         const bool  Valid( void ) const { 
00114             return m_explicitelyInitialized; 
00115         }
00116 
00117     private:
00118         static const DWORD32 kMagicNumber1, kMagicNumber2;
00119         static const DWORD32 HALF_RAND_MAX;
00120         static const double kIntMax, kIntMax1, kHalfIntMax;
00121         DWORD32 m_randar[256];
00122         DWORD32 m_randX, m_randY;
00123         bool m_explicitelyInitialized;
00124 };
00125 
00126 
00127