RANDOM.H 10 KB

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