glrandom.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
00023 */
00024 
00025 
00026 #ifndef GRINLIZ_RANDOM_INCLUDED
00027 #define GRINLIZ_RANDOM_INCLUDED
00028 
00029 #include "gltypes.h"
00030 #include "gldebug.h"
00031 
00032 namespace grinliz {
00033 
00039 class Random
00040 {
00041   public:
00043         Random( U32 _seed = 0 );
00044 
00050         void SetSeed( U32 _seed );
00051 
00053         U16 Rand()                                              { return Randomize();   }
00057         U16 Rand( U16 upperBound )              { return Randomize() % upperBound; }
00058 
00062         U16 RandD2( U16 upperBound )    { U16 d1 = upperBound / 2 + 1;
00063                                                                           U16 d2 = upperBound - d1 + 1;
00064                                                                           U16 r  = Rand( d1 ) + Rand( d2 );
00065                                                                           GLASSERT( r < upperBound );
00066                                                                           return r;
00067                                                                         }
00068 
00072         U16 RandD3( U16 upperBound )    { U16 d1 = upperBound / 3 + 2;
00073                                                                           U16 d2 = upperBound / 3 + 2;
00074                                                                           U16 d3 = upperBound - d1 - d2 + 2;
00075                                                                           U16 r = Rand( d1 ) + Rand( d2 ) + Rand( d3 );
00076                                                                           GLASSERT( r < upperBound );
00077                                                                           return r;
00078                                                                         }
00079 
00081         double DRand( double upper )    { return upper * double( Randomize() ) / 65535.0; }
00082 
00084         float FRand( float upper )              { return upper * float( Randomize() ) / 65535.0f; }
00085 
00087         bool Boolean()                                  { return Rand( 100 ) >= 50; }
00088 
00089   private:
00090         U32 seed;
00091         inline void CalcSeed()          { seed = seed * 39421 + 1; }
00092         U16 Randomize();
00093 
00094         enum {
00095                 TABLESIZE = 16,
00096                 SLOTWIDTH = 0x10000 / TABLESIZE
00097         };
00098 
00099         U16 seedTable[ TABLESIZE ];
00100 };
00101 
00102 };      // namespace grinliz
00103 
00104 #endif

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7