Random.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MATH_RANDOM_H__
  21. #define __MATH_RANDOM_H__
  22. /*
  23. ===============================================================================
  24. Random number generator
  25. ===============================================================================
  26. */
  27. class idRandom {
  28. public:
  29. idRandom( int seed = 0 );
  30. void SetSeed( int seed );
  31. int GetSeed( void ) const;
  32. int RandomInt( void ); // random integer in the range [0, MAX_RAND]
  33. int RandomInt( int max ); // random integer in the range [0, max[
  34. float RandomFloat( void ); // random number in the range [0.0f, 1.0f]
  35. float CRandomFloat( void ); // random number in the range [-1.0f, 1.0f]
  36. static const int MAX_RAND = 0x7fff;
  37. private:
  38. int seed;
  39. };
  40. ID_INLINE idRandom::idRandom( int seed ) {
  41. this->seed = seed;
  42. }
  43. ID_INLINE void idRandom::SetSeed( int seed ) {
  44. this->seed = seed;
  45. }
  46. ID_INLINE int idRandom::GetSeed( void ) const {
  47. return seed;
  48. }
  49. ID_INLINE int idRandom::RandomInt( void ) {
  50. seed = 69069 * seed + 1;
  51. return ( seed & idRandom::MAX_RAND );
  52. }
  53. ID_INLINE int idRandom::RandomInt( int max ) {
  54. if ( max == 0 ) {
  55. return 0; // avoid divide by zero error
  56. }
  57. return RandomInt() % max;
  58. }
  59. ID_INLINE float idRandom::RandomFloat( void ) {
  60. return ( RandomInt() / ( float )( idRandom::MAX_RAND + 1 ) );
  61. }
  62. ID_INLINE float idRandom::CRandomFloat( void ) {
  63. return ( 2.0f * ( RandomFloat() - 0.5f ) );
  64. }
  65. /*
  66. ===============================================================================
  67. Random number generator
  68. ===============================================================================
  69. */
  70. class idRandom2 {
  71. public:
  72. idRandom2( unsigned long seed = 0 );
  73. void SetSeed( unsigned long seed );
  74. unsigned long GetSeed( void ) const;
  75. int RandomInt( void ); // random integer in the range [0, MAX_RAND]
  76. int RandomInt( int max ); // random integer in the range [0, max]
  77. float RandomFloat( void ); // random number in the range [0.0f, 1.0f]
  78. float CRandomFloat( void ); // random number in the range [-1.0f, 1.0f]
  79. static const int MAX_RAND = 0x7fff;
  80. private:
  81. unsigned long seed;
  82. static const unsigned long IEEE_ONE = 0x3f800000;
  83. static const unsigned long IEEE_MASK = 0x007fffff;
  84. };
  85. ID_INLINE idRandom2::idRandom2( unsigned long seed ) {
  86. this->seed = seed;
  87. }
  88. ID_INLINE void idRandom2::SetSeed( unsigned long seed ) {
  89. this->seed = seed;
  90. }
  91. ID_INLINE unsigned long idRandom2::GetSeed( void ) const {
  92. return seed;
  93. }
  94. ID_INLINE int idRandom2::RandomInt( void ) {
  95. seed = 1664525L * seed + 1013904223L;
  96. return ( (int) seed & idRandom2::MAX_RAND );
  97. }
  98. ID_INLINE int idRandom2::RandomInt( int max ) {
  99. if ( max == 0 ) {
  100. return 0; // avoid divide by zero error
  101. }
  102. return ( RandomInt() >> ( 16 - idMath::BitsForInteger( max ) ) ) % max;
  103. }
  104. ID_INLINE float idRandom2::RandomFloat( void ) {
  105. unsigned long i;
  106. seed = 1664525L * seed + 1013904223L;
  107. i = idRandom2::IEEE_ONE | ( seed & idRandom2::IEEE_MASK );
  108. return ( ( *(float *)&i ) - 1.0f );
  109. }
  110. ID_INLINE float idRandom2::CRandomFloat( void ) {
  111. unsigned long i;
  112. seed = 1664525L * seed + 1013904223L;
  113. i = idRandom2::IEEE_ONE | ( seed & idRandom2::IEEE_MASK );
  114. return ( 2.0f * ( *(float *)&i ) - 3.0f );
  115. }
  116. #endif /* !__MATH_RANDOM_H__ */