rand.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "random.h"
  10. #include <wincrypt.h>
  11. /* Period parameters */
  12. #define N 624
  13. #define M 397
  14. #define MATRIX_A 0x9908b0dfUL /* constant vector a */
  15. #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
  16. #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
  17. static unsigned long mt[N]; /* the array for the state vector */
  18. static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
  19. static CRITICAL_SECTION randCS;
  20. /* initializes mt[N] with a seed */
  21. void init_genrand(unsigned long s)
  22. {
  23. mt[0] = s & 0xffffffffUL;
  24. for (mti = 1; mti < N; mti++)
  25. {
  26. mt[mti] =
  27. (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
  28. mt[mti] &= 0xffffffffUL;
  29. }
  30. }
  31. /* initialize by an array with array-length */
  32. /* init_key is the array for initializing keys */
  33. /* key_length is its length */
  34. /* slight change for C++, 2004/2/26 */
  35. void init_by_array(unsigned long init_key[], int key_length)
  36. {
  37. int i, j, k;
  38. init_genrand(19650218UL);
  39. i=1; j=0;
  40. k = (N>key_length ? N : key_length);
  41. for (; k; k--) {
  42. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
  43. + init_key[j] + j; /* non linear */
  44. mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  45. i++; j++;
  46. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  47. if (j>=key_length) j=0;
  48. }
  49. for (k=N-1; k; k--) {
  50. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
  51. - i; /* non linear */
  52. mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  53. i++;
  54. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  55. }
  56. mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
  57. }
  58. static const unsigned long mag01[2] = {0x0UL, MATRIX_A};
  59. /* mag01[x] = x * MATRIX_A for x=0,1 */
  60. /* generates a random number on [0,0xffffffff]-interval */
  61. unsigned long genrand_int32(void)
  62. {
  63. EnterCriticalSection(&randCS);
  64. unsigned long y;
  65. if (mti >= N)
  66. { /* generate N words at one time */
  67. int kk;
  68. // benski> CUT - because we call init_genrand() as the first thing in WinMain
  69. //if (mti == N+1) /* if init_genrand() has not been called, */
  70. // benski> CUT init_genrand(5489UL); /* a default initial seed is used */
  71. for (kk = 0;kk < N - M;kk++)
  72. {
  73. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  74. mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
  75. }
  76. for (;kk < N - 1;kk++)
  77. {
  78. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  79. mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
  80. }
  81. y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  82. mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  83. mti = 0;
  84. }
  85. y = mt[mti++];
  86. LeaveCriticalSection(&randCS);
  87. /* Tempering */
  88. y ^= (y >> 11);
  89. y ^= (y << 7) & 0x9d2c5680UL;
  90. y ^= (y << 15) & 0xefc60000UL;
  91. y ^= (y >> 18);
  92. return y;
  93. }
  94. /* generates a random number on [0,0x7fffffff]-interval */
  95. long genrand_int31(void)
  96. {
  97. return (long)(genrand_int32() >> 1);
  98. }
  99. static HCRYPTPROV GetKeySet()
  100. {
  101. HCRYPTPROV hCryptProv;
  102. LPCSTR UserName = "WinampKeyContainer"; // name of the key container
  103. if(CryptAcquireContextA(&hCryptProv, UserName, NULL, PROV_RSA_FULL, 0))
  104. {
  105. return hCryptProv;
  106. }
  107. else if(CryptAcquireContextA(&hCryptProv, UserName, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
  108. {
  109. return hCryptProv;
  110. }
  111. else
  112. return 0;
  113. }
  114. Random::Random()
  115. {
  116. InitializeCriticalSectionAndSpinCount(&randCS, 1000);
  117. HCRYPTPROV hCryptProv = GetKeySet();
  118. if (hCryptProv)
  119. {
  120. unsigned long seed_data[8];
  121. if (CryptGenRandom(hCryptProv, sizeof(seed_data), (PBYTE)seed_data))
  122. {
  123. init_by_array(seed_data, 8);
  124. }
  125. CryptReleaseContext(hCryptProv,0);
  126. }
  127. else
  128. {
  129. LARGE_INTEGER performance_counter;
  130. QueryPerformanceCounter(&performance_counter);
  131. unsigned long seed_data[4] = {(unsigned long)performance_counter.HighPart, (unsigned long)performance_counter.LowPart, (unsigned long)GetTickCount64(), (unsigned long)GetCurrentProcessId()};
  132. init_by_array(seed_data, 4);
  133. }
  134. }
  135. int warand()
  136. {
  137. return genrand_int31();
  138. }
  139. float warandf()
  140. {
  141. return genrand_int32()*(1.0f / 4294967295.0f);
  142. }
  143. RandomGenerator Random::GetFunction()
  144. {
  145. return warand;
  146. }
  147. UnsignedRandomGenerator Random::GetUnsignedFunction()
  148. {
  149. return genrand_int32;
  150. }
  151. int Random::GetNumber()
  152. {
  153. return genrand_int32();
  154. }
  155. int Random::GetPositiveNumber()
  156. {
  157. return genrand_int31();
  158. }
  159. /* generates a random number on [0,1]-real-interval */
  160. float Random::GetFloat()
  161. {
  162. return genrand_int32()*(1.0f / 4294967295.0f);
  163. /* divided by 2^32-1 */
  164. }
  165. /* generates a random number on [0,1)-real-interval */
  166. float Random::GetFloat_LessThanOne()
  167. {
  168. return genrand_int32()*(1.0f / 4294967296.0f);
  169. /* divided by 2^32 */
  170. }
  171. /* generates a random number on (0,1)-real-interval */
  172. float Random::GetFloat_LessThanOne_NotZero()
  173. {
  174. return (((float)genrand_int32()) + 0.5f)*(1.0f / 4294967296.0f);
  175. } // (0-1)
  176. /* generates a random number on [0,1) with 53-bit resolution*/
  177. double Random::GetDouble()
  178. {
  179. unsigned long a = genrand_int32() >> 5, b = genrand_int32() >> 6;
  180. return (a*67108864.0 + b)*(1.0 / 9007199254740992.0);
  181. }
  182. #ifdef CBCLASS
  183. #undef CBCLASS
  184. #endif
  185. #define CBCLASS Random
  186. START_DISPATCH;
  187. CB(API_RANDOM_GETFUNCTION, GetFunction)
  188. CB(API_RANDOM_GETFUNCTION_UNSIGNED, GetUnsignedFunction)
  189. CB(API_RANDOM_GETNUMBER, GetNumber)
  190. CB(API_RANDOM_GETPOSITIVENUMBER, GetPositiveNumber)
  191. CB(API_RANDOM_GETFLOAT, GetFloat)
  192. CB(API_RANDOM_GETFLOAT2, GetFloat_LessThanOne)
  193. CB(API_RANDOM_GETFLOAT3, GetFloat_LessThanOne_NotZero)
  194. CB(API_RANDOM_GETDOUBLE, GetDouble)
  195. END_DISPATCH