Main Page   Class Hierarchy   Compound List   File List   Compound Members  

glrandom.h

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

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001