RANDOM.CPP 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/RANDOM.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : RAND.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 02/28/96 *
  30. * *
  31. * Last Update : February 28, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * RandomClass::RandomClass -- Constructor for the random number class. *
  36. * RandomClass::operator() -- Fetches the next random number in the sequence. *
  37. * RandomClass::operator() -- Ranged random number generator. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "random.h"
  40. #ifdef RANDOM_COUNT
  41. #include <stdio.h>
  42. extern long Frame;
  43. #endif
  44. /***********************************************************************************************
  45. * RandomClass::RandomClass -- Constructor for the random number class. *
  46. * *
  47. * This constructor can take an integer as a parameter. This allows the class to be *
  48. * constructed by assigning an integer to an existing object. The compiler creates a *
  49. * temporary with the constructor and then performs a copy constructor operation. *
  50. * *
  51. * INPUT: seed -- The optional starting seed value to use. *
  52. * *
  53. * OUTPUT: none *
  54. * *
  55. * WARNINGS: none *
  56. * *
  57. * HISTORY: *
  58. * 02/27/1996 JLB : Created. *
  59. *=============================================================================================*/
  60. RandomClass::RandomClass(unsigned seed) :
  61. Seed(seed)
  62. {
  63. #ifdef RANDOM_COUNT
  64. Count1 = 0;
  65. Count2 = 0;
  66. #endif
  67. }
  68. /***********************************************************************************************
  69. * RandomClass::operator() -- Fetches the next random number in the sequence. *
  70. * *
  71. * This routine will fetch the next random number in the sequence. *
  72. * *
  73. * INPUT: none *
  74. * *
  75. * OUTPUT: Returns with the next random number. *
  76. * *
  77. * WARNINGS: This routine modifies the seed value so that subsequent calls will not return *
  78. * the same value. Take note that this routine only returns 15 bits of *
  79. * random number. *
  80. * *
  81. * HISTORY: *
  82. * 02/27/1996 JLB : Created. *
  83. *=============================================================================================*/
  84. int RandomClass::operator ()(void)
  85. {
  86. #ifdef RANDOM_COUNT
  87. Count1++;
  88. printf("Frame %d: Random Count1:%d Count2:%d (%x)\n",Frame,Count1,Count2,Seed);
  89. #endif
  90. /*
  91. ** Transform the seed value into the next number in the sequence.
  92. */
  93. Seed = (Seed * MULT_CONSTANT) + ADD_CONSTANT;
  94. /*
  95. ** Extract the 'random' bits from the seed and return that value as the
  96. ** random number result.
  97. */
  98. return((Seed >> THROW_AWAY_BITS) & (~((~0) << SIGNIFICANT_BITS)));
  99. }
  100. /***********************************************************************************************
  101. * RandomClass::operator() -- Ranged random number generator. *
  102. * *
  103. * This function will return with a random number within the range specified. This replaces *
  104. * the functionality of IRandom() in the old library. *
  105. * *
  106. * INPUT: minval -- The minimum value to return from the function. *
  107. * *
  108. * maxval -- The maximum value to return from the function. *
  109. * *
  110. * OUTPUT: Returns with a random number that falls between the minval and maxval (inclusive). *
  111. * *
  112. * WARNINGS: The range specified must fall within the maximum bit significance of the *
  113. * random number algorithm (15 bits), otherwise the value returned will be *
  114. * decidedly non-random. *
  115. * *
  116. * HISTORY: *
  117. * 02/27/1996 JLB : Created. *
  118. *=============================================================================================*/
  119. int RandomClass::operator() (int minval, int maxval)
  120. {
  121. #ifdef RANDOM_COUNT
  122. Count2++;
  123. printf("Frame %d: Random Count1:%d Count2:%d (%x)\n",Frame,Count1,Count2,Seed);
  124. #endif
  125. /*
  126. ** Test for shortcut case where the range is null and thus
  127. ** the number to return is actually implicit from the
  128. ** parameters.
  129. */
  130. if (minval == maxval) return(minval);
  131. /*
  132. ** Ensure that the min and max range values are in proper order.
  133. */
  134. if (minval > maxval) {
  135. int temp = minval;
  136. minval = maxval;
  137. maxval = temp;
  138. }
  139. /*
  140. ** Find the highest bit that fits within the magnitude of the
  141. ** range of random numbers desired. Notice that the scan is
  142. ** limited to the range of significant bits returned by the
  143. ** random number algorithm.
  144. */
  145. int magnitude = maxval - minval;
  146. int highbit = SIGNIFICANT_BITS-1;
  147. while ((magnitude & (1 << highbit)) == 0 && highbit > 0) {
  148. highbit--;
  149. }
  150. /*
  151. ** Create a full bit mask pattern that has all bits set that just
  152. ** barely covers the magnitude of the number range desired.
  153. */
  154. int mask = ~( (~0L) << (highbit+1) );
  155. /*
  156. ** Keep picking random numbers until it fits within the magnitude desired.
  157. */
  158. int pick = magnitude+1;
  159. while (pick > magnitude) {
  160. pick = operator()() & mask;
  161. }
  162. /*
  163. ** Finally, bias the random number pick to the start of the range
  164. ** requested.
  165. */
  166. return(pick + minval);
  167. }