rand.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "rand.h"
  19. #include <cmath>
  20. static const double theMultFactor = 1.0 / (pow(2, 8 * sizeof(unsigned int)) - 1.0);
  21. RandClass::RandClass(int start)
  22. {
  23. seed[0] = 0xf22d0e56L;
  24. seed[1] = 0x883126e9L;
  25. seed[2] = 0xc624dd2fL;
  26. seed[3] = 0x702c49cL;
  27. seed[4] = 0x9e353f7dL;
  28. seed[5] = 0x6fdf3b64L;
  29. unsigned int ax;
  30. ax = start; /* mov eax,SEED */
  31. ax += 0xf22d0e56; /* add eax,0f22d0e56h */
  32. seed[0] = ax; /* mov seed,eax */
  33. ax += 0x883126e9 - 0xf22d0e56; /* add eax,0883126e9h-0f22d0e56h */
  34. seed[1] = ax; /* mov seed+4,eax */
  35. ax += 0xc624dd2f - 0x883126e9; /* add eax,0c624dd2fh-0883126e9h */
  36. seed[2] = ax; /* mov seed+8,eax */
  37. ax += 0x0702c49c - 0xc624dd2f; /* add eax,00702c49ch-0c624dd2fh */
  38. seed[3] = ax; /* mov seed+12,eax */
  39. ax += 0x9e353f7d - 0x0702c49c; /* add eax,09e353f7dh-00702c49ch */
  40. seed[4] = ax; /* mov seed+16,eax */
  41. ax += 0x6fdf3b64 - 0x9e353f7d; /* add eax,06fdf3b64h-09e353f7dh */
  42. seed[5] = ax; /* mov seed+20,eax */
  43. }
  44. // Add with carry. SUM is replaced with A + B + C, C is replaced with 1 if there was a carry, 0 if there wasn't. A carry occurred if the sum is less than one of the inputs. This is addition, so carry can never be more than one.
  45. #define ADC(SUM, A, B, C) SUM = (A) + (B) + (C); C = ((SUM < (A)) || (SUM < (B)))
  46. unsigned int RandClass::randomValue( void )
  47. {
  48. unsigned int ax;
  49. unsigned int c = 0;
  50. ADC(ax, seed[5], seed[4], c); /* mov ax,seed+20 */
  51. /* add ax,seed+16 */
  52. seed[4] = ax; /* mov seed+8,ax */
  53. ADC(ax, ax, seed[3], c); /* adc ax,seed+12 */
  54. seed[3] = ax; /* mov seed+12,ax */
  55. ADC(ax, ax, seed[2], c); /* adc ax,seed+8 */
  56. seed[2] = ax; /* mov seed+8,ax */
  57. ADC(ax, ax, seed[1], c); /* adc ax,seed+4 */
  58. seed[1] = ax; /* mov seed+4,ax */
  59. ADC(ax, ax, seed[0], c); /* adc ax,seed+0 */
  60. seed[0] = ax; /* mov seed+0,ax */
  61. /* Increment seed array, bubbling up the carries. */
  62. if (!++seed[5])
  63. {
  64. if (!++seed[4])
  65. {
  66. if (!++seed[3])
  67. {
  68. if (!++seed[2])
  69. {
  70. if (!++seed[1])
  71. {
  72. ++seed[0];
  73. ++ax;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return(ax);
  80. }
  81. int RandClass::Int(void)
  82. {
  83. return (int)randomValue();
  84. }
  85. int RandClass::Int(int low, int high)
  86. {
  87. unsigned int delta = high - low + 1;
  88. int rval;
  89. if (delta == 0)
  90. return high;
  91. rval = ((int)(randomValue()%delta)) + low;
  92. return rval;
  93. }
  94. double RandClass::Double(void)
  95. {
  96. return Double(0.0, 1.0);
  97. }
  98. double RandClass::Double(double low, double high)
  99. {
  100. double delta = high - low;
  101. double rval;
  102. if (delta <= 0.0)
  103. return high;
  104. rval = ((double)(randomValue()) * theMultFactor) * delta + low;
  105. return rval;
  106. }