RANDOM.CPP 9.6 KB

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