random.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/random.h $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 8/29/01 10:24p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Pick_Random_Number -- Picks a random number between two values (inclusive). *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if _MSC_VER >= 1000
  37. #pragma once
  38. #endif // _MSC_VER >= 1000
  39. #ifndef RANDOM_H
  40. #define RANDOM_H
  41. /*
  42. ** This class functions like a 'magic' int value that returns a random number
  43. ** every time it is read. To set the "random seed" for this, just assign a number
  44. ** to the object (just as you would if it were a true 'int' value). Take note that although
  45. ** this class will return an 'int', the actual significance of the random number is
  46. ** limited to 15 bits (0..32767).
  47. */
  48. class RandomClass {
  49. public:
  50. RandomClass(unsigned seed=0);
  51. operator int(void) {return(operator()());};
  52. int operator() (void);
  53. int operator() (int minval, int maxval);
  54. enum {
  55. SIGNIFICANT_BITS=15 // Random number bit significance.
  56. };
  57. protected:
  58. unsigned long Seed;
  59. /*
  60. ** Internal working constants that are used to generate the next
  61. ** random number.
  62. */
  63. enum {
  64. MULT_CONSTANT=0x41C64E6D, // K multiplier value.
  65. ADD_CONSTANT=0x00003039, // K additive value.
  66. THROW_AWAY_BITS=10 // Low bits to throw away.
  67. };
  68. };
  69. /*
  70. ** This class functions like a 'magic' number where it returns a different value every
  71. ** time it is read. It is nearly identical in function to the RandomClass, but has the
  72. ** following improvements.
  73. **
  74. ** 1> It generates random numbers very quickly. No multiplies are
  75. ** used in the algorithm.
  76. **
  77. ** 2> The return value is a full 32 bits rather than 15 bits of
  78. ** the RandomClass.
  79. **
  80. ** 3> The bit pattern won't ever repeat. (actually it will repeat
  81. ** in about 10 to the 50th power times).
  82. */
  83. // WARNING!!!!
  84. // This random number generator starts breaking down in 64 dimensions
  85. // behaving very badly in that domain
  86. // HY 6/14/01
  87. class Random2Class {
  88. public:
  89. Random2Class(unsigned seed=0);
  90. operator int(void) {return(operator()());};
  91. int operator() (void);
  92. int operator() (int minval, int maxval);
  93. enum {
  94. SIGNIFICANT_BITS=32 // Random number bit significance.
  95. };
  96. protected:
  97. int Index1;
  98. int Index2;
  99. int Table[250];
  100. };
  101. /*
  102. ** This class functions like a 'magic' number where it returns a different value every
  103. ** time it is read. It is nearly identical in function to the RandomClass and Random2Class,
  104. ** but has the following improvements.
  105. **
  106. ** 1> The random number returned is very strongly random. Approaching cryptographic
  107. ** quality.
  108. **
  109. ** 2> The return value is a full 32 bits rather than 15 bits of
  110. ** the RandomClass.
  111. **
  112. ** 3> The bit pattern won't repeat until 2^32 times.
  113. */
  114. // WARNING!!!!
  115. // This random number generator starts breaking down in 3 dimensions
  116. // exhibiting a strange bias
  117. // HY 6/14/01
  118. class Random3Class {
  119. public:
  120. Random3Class(unsigned seed1=0, unsigned seed2=0);
  121. operator int(void) {return(operator()());};
  122. int operator() (void);
  123. int operator() (int minval, int maxval);
  124. enum {
  125. SIGNIFICANT_BITS=32 // Random number bit significance.
  126. };
  127. protected:
  128. static int Mix1[20];
  129. static int Mix2[20];
  130. int Seed;
  131. int Index;
  132. };
  133. /*
  134. ** This class functions like a 'magic' number where it returns a different value every
  135. ** time it is read. It is based on the paper "Mersenne Twister: A 623-Dimensionally
  136. ** Equidistributed Uniform Pseudo-Random Number Generator
  137. ** by Makoto Matsumoto and Takuji Nishimura, ACM Transactions on Modeling and
  138. ** Computer Simulation, Vol 8, No 1, January 1998, Pages 3-30
  139. ** Hector Yee
  140. **
  141. ** 1> The random number returned is very strongly random
  142. **
  143. ** 2> The return value is a full 32 bits rather than 15 bits of
  144. ** the RandomClass.
  145. **
  146. ** 3> The bit pattern won't repeat until 2^19937 -1 times
  147. **
  148. */
  149. // WARNING!!!!
  150. // Do not use for cryptography. This random number generator is good for numerical
  151. // simulation. Optimized, it's 4 times faster than rand()
  152. // http://www.math.keio.ac.jp/~matumoto/emt.html
  153. // HY 6/14/01
  154. class Random4Class {
  155. public:
  156. Random4Class(unsigned int seed=4357);
  157. operator int(void) {return(operator()());};
  158. int operator() (void);
  159. int operator() (int minval, int maxval);
  160. float Get_Float();
  161. enum {
  162. SIGNIFICANT_BITS=32 // Random number bit significance.
  163. };
  164. protected:
  165. unsigned int mt[624]; // state vector
  166. int mti; // index
  167. };
  168. /***********************************************************************************************
  169. * Pick_Random_Number -- Picks a random number between two values (inclusive). *
  170. * *
  171. * This is a utility template that works with one of the random number classes. Given a *
  172. * random number generator, this routine will return with a value that lies between the *
  173. * minimum and maximum values specified (subject to the bit precision limitations of the *
  174. * random number generator itself). *
  175. * *
  176. * INPUT: generator -- Reference to the random number generator to use for the pick *
  177. * process. *
  178. * *
  179. * minval -- The minimum legal return value (inclusive). *
  180. * *
  181. * maxval -- The maximum legal return value (inclusive). *
  182. * *
  183. * OUTPUT: Returns with a random number between the minval and maxval (inclusive). *
  184. * *
  185. * WARNINGS: none *
  186. * *
  187. * HISTORY: *
  188. * 05/23/1997 JLB : Created. *
  189. *=============================================================================================*/
  190. template<class T>
  191. int Pick_Random_Number(T & generator, int minval, int maxval)
  192. {
  193. /*
  194. ** Test for shortcut case where the range is null and thus
  195. ** the number to return is actually implicit from the
  196. ** parameters.
  197. */
  198. if (minval == maxval) return(minval);
  199. /*
  200. ** Ensure that the min and max range values are in proper order.
  201. */
  202. if (minval > maxval) {
  203. int temp = minval;
  204. minval = maxval;
  205. maxval = temp;
  206. }
  207. /*
  208. ** Find the highest bit that fits within the magnitude of the
  209. ** range of random numbers desired. Notice that the scan is
  210. ** limited to the range of significant bits returned by the
  211. ** random number algorithm.
  212. */
  213. int magnitude = maxval - minval;
  214. int highbit = T::SIGNIFICANT_BITS-1;
  215. while ((magnitude & (1 << highbit)) == 0 && highbit > 0) {
  216. highbit--;
  217. }
  218. /*
  219. ** Create a full bit mask pattern that has all bits set that just
  220. ** barely covers the magnitude of the number range desired.
  221. */
  222. int mask = ~( (~0L) << (highbit+1));
  223. /*
  224. ** Keep picking random numbers until it fits within the magnitude desired. With a
  225. ** good random number generator, it will have to perform this loop an average
  226. ** of one and a half times.
  227. */
  228. int pick = magnitude+1;
  229. while (pick > magnitude) {
  230. pick = generator() & mask;
  231. }
  232. /*
  233. ** Finally, bias the random number pick to the start of the range
  234. ** requested.
  235. */
  236. return(pick + minval);
  237. }
  238. #endif