home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / math / Random.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  3KB  |  132 lines

  1.  
  2. #ifndef __MATH_RANDOM_H__
  3. #define __MATH_RANDOM_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     Random number generator
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. class idRandom {
  14. public:
  15.                         idRandom( int seed = 0 );
  16.  
  17.     void                SetSeed( int seed );
  18.     int                    GetSeed( void ) const;
  19.  
  20.     int                    RandomInt( void );            // random integer in the range [0, MAX_RAND]
  21.     int                    RandomInt( int max );        // random integer in the range [0, max[
  22.     float                RandomFloat( void );        // random number in the range [0.0f, 1.0f]
  23.     float                CRandomFloat( void );        // random number in the range [-1.0f, 1.0f]
  24.  
  25.     static const int    MAX_RAND = 0x7fff;
  26.  
  27. private:
  28.     int                    seed;
  29. };
  30.  
  31. ID_INLINE idRandom::idRandom( int seed ) {
  32.     this->seed = seed;
  33. }
  34.  
  35. ID_INLINE void idRandom::SetSeed( int seed ) {
  36.     this->seed = seed;
  37. }
  38.  
  39. ID_INLINE int idRandom::GetSeed( void ) const {
  40.     return seed;
  41. }
  42.  
  43. ID_INLINE int idRandom::RandomInt( void ) {
  44.     seed = 69069 * seed + 1;
  45.     return ( seed & idRandom::MAX_RAND );
  46. }
  47.  
  48. ID_INLINE int idRandom::RandomInt( int max ) {
  49.     if ( max == 0 ) {
  50.         return 0;            // avoid divide by zero error
  51.     }
  52.     return RandomInt() % max;
  53. }
  54.  
  55. ID_INLINE float idRandom::RandomFloat( void ) {
  56.     return ( RandomInt() / ( float )( idRandom::MAX_RAND + 1 ) );
  57. }
  58.  
  59. ID_INLINE float idRandom::CRandomFloat( void ) {
  60.     return ( 2.0f * ( RandomFloat() - 0.5f ) );
  61. }
  62.  
  63.  
  64. /*
  65. ===============================================================================
  66.  
  67.     Random number generator
  68.  
  69. ===============================================================================
  70. */
  71.  
  72. class idRandom2 {
  73. public:
  74.                             idRandom2( unsigned long seed = 0 );
  75.  
  76.     void                    SetSeed( unsigned long seed );
  77.     unsigned long            GetSeed( void ) const;
  78.  
  79.     int                        RandomInt( void );            // random integer in the range [0, MAX_RAND]
  80.     int                        RandomInt( int max );        // random integer in the range [0, max]
  81.     float                    RandomFloat( void );        // random number in the range [0.0f, 1.0f]
  82.     float                    CRandomFloat( void );        // random number in the range [-1.0f, 1.0f]
  83.  
  84.     static const int        MAX_RAND = 0x7fff;
  85.  
  86. private:
  87.     unsigned long            seed;
  88.  
  89.     static const unsigned long    IEEE_ONE = 0x3f800000;
  90.     static const unsigned long    IEEE_MASK = 0x007fffff;
  91. };
  92.  
  93. ID_INLINE idRandom2::idRandom2( unsigned long seed ) {
  94.     this->seed = seed;
  95. }
  96.  
  97. ID_INLINE void idRandom2::SetSeed( unsigned long seed ) {
  98.     this->seed = seed;
  99. }
  100.  
  101. ID_INLINE unsigned long idRandom2::GetSeed( void ) const {
  102.     return seed;
  103. }
  104.  
  105. ID_INLINE int idRandom2::RandomInt( void ) {
  106.     seed = 1664525L * seed + 1013904223L;
  107.     return ( (int) seed & idRandom2::MAX_RAND );
  108. }
  109.  
  110. ID_INLINE int idRandom2::RandomInt( int max ) {
  111.     if ( max == 0 ) {
  112.         return 0;        // avoid divide by zero error
  113.     }
  114.     return ( RandomInt() >> ( 16 - idMath::BitsForInteger( max ) ) ) % max;
  115. }
  116.  
  117. ID_INLINE float idRandom2::RandomFloat( void ) {
  118.     unsigned long i;
  119.     seed = 1664525L * seed + 1013904223L;
  120.     i = idRandom2::IEEE_ONE | ( seed & idRandom2::IEEE_MASK );
  121.     return ( ( *(float *)&i ) - 1.0f );
  122. }
  123.  
  124. ID_INLINE float idRandom2::CRandomFloat( void ) {
  125.     unsigned long i;
  126.     seed = 1664525L * seed + 1013904223L;
  127.     i = idRandom2::IEEE_ONE | ( seed & idRandom2::IEEE_MASK );
  128.     return ( 2.0f * ( *(float *)&i ) - 3.0f );
  129. }
  130.  
  131. #endif /* !__MATH_RANDOM_H__ */
  132.