Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
7.7 KiB

  1. /*
  2. * random.h
  3. *
  4. * Copyright (c) 1993-1995 by DataBeam Corporation, Lexington, KY
  5. *
  6. * Abstract:
  7. * This is the interface file for the RandomNumberGenerator class.
  8. * Instances of this class can generate random numbers within a specifed
  9. * range on demand. Many of these objects can exist at once, and they
  10. * will not interfere with each other.
  11. *
  12. * Caveats:
  13. * None.
  14. *
  15. * Author:
  16. * James J. Johnstone IV
  17. */
  18. #ifndef _RANDOM_
  19. #define _RANDOM_
  20. /*
  21. * The data type of the return value of the RandomNumberGenerator class.
  22. */
  23. typedef ULong RandomValue;
  24. #ifdef USE_RANDOM_CLASS
  25. /*
  26. * This typedef is an enumeration of all possible random number generation
  27. * algorithms and is used when constructing a new random number generator.
  28. * See "Numerical Recipes in 'C'" for details as to the difference between
  29. * the various algorithms.
  30. */
  31. typedef enum
  32. {
  33. ALGORITHM_RAN1,
  34. ALGORITHM_RANQD2,
  35. ALGORITHM_RAN4
  36. } Algorithm;
  37. typedef Algorithm * PAlgorithm;
  38. /*
  39. * The default algorithm for the random number generator object.
  40. */
  41. #define DEFAULT_ALGORITHM ALGORITHM_RAN1
  42. /*
  43. * If specified as the seed value, a random seed will be generated by the
  44. * random number generator.
  45. */
  46. #define RANDOM_SEED 0
  47. /*
  48. * Defines for ran1() algorithm from "Numerical Recipes in 'C'"
  49. */
  50. #define IA 16807
  51. #define IM 2147483647L
  52. #define AM (1.0/IM)
  53. #define IQ 127773L
  54. #define IR 2836
  55. #define NTAB 32
  56. #define NDIV (1+(IM-1)/NTAB)
  57. #define EPS 1.2e-7
  58. #define RNMX (1.0-EPS)
  59. /*
  60. * Defines for ranqd2() algorithm from "Numerical Recipes in 'C'"
  61. */
  62. #define RANQD2_A 1664525L
  63. #define RANQD2_C 1013904223L
  64. /*
  65. * Defines for ranqd2() and ran4() algorithms from "Numerical Recipes in 'C'"
  66. */
  67. #define JFLONE 0x3f800000L
  68. #define JFLMSK 0x007fffffL
  69. /*
  70. * Defines for the ran4() algorithm from "Numerical Recipes in 'C'"
  71. */
  72. #define NITER 4
  73. /*
  74. * The definition of the RandomNumberGenerator class.
  75. */
  76. class RandomNumberGenerator
  77. {
  78. public:
  79. RandomNumberGenerator ();
  80. RandomNumberGenerator (
  81. ULong seed);
  82. RandomNumberGenerator (
  83. Algorithm algorithm);
  84. RandomNumberGenerator (
  85. Algorithm algorithm,
  86. ULong seed);
  87. virtual ~RandomNumberGenerator ();
  88. RandomValue GetRandomNumber (
  89. RandomValue lo_extent,
  90. RandomValue hi_extent);
  91. Void Reseed ();
  92. Void Reseed (
  93. ULong seed);
  94. private:
  95. Void GenerateSeed (
  96. ULong seed);
  97. Float RAN1UniformDeviate ();
  98. Float RAN4UniformDeviate ();
  99. Void PseudoDESHashing (
  100. ULong *lword,
  101. ULong *irword);
  102. Algorithm Algorithm_In_Use;
  103. Long Running_Random_Number;
  104. };
  105. typedef RandomNumberGenerator * PRandomNumberGenerator;
  106. /*
  107. * RandomNumberGenerator ()
  108. *
  109. * Functional Description:
  110. * This version of the constructor is used to create a random number
  111. * generator object that has been automatically seeded with the current
  112. * time. The default algorithm will be used.
  113. *
  114. * Formal Parameters:
  115. * None.
  116. *
  117. * Return Value:
  118. * None.
  119. *
  120. * Side Effects:
  121. * None.
  122. *
  123. * Caveats:
  124. * None.
  125. */
  126. /*
  127. * RandomNumberGenerator (
  128. * ULong seed)
  129. *
  130. * Functional Description:
  131. * This version of the constructor is used to create a random number
  132. * generator object which is seeded with the supplied value. The default
  133. * algorithm will be used.
  134. *
  135. * Formal Parameters:
  136. * seed (i)
  137. * A value used to seed the random number generator. If the seed value
  138. * is zero, the random number generator object will use a random seed
  139. * value based on the time.
  140. *
  141. * Return Value:
  142. * None.
  143. *
  144. * Side Effects:
  145. * None.
  146. *
  147. * Caveats:
  148. * None.
  149. */
  150. /*
  151. * RandomNumberGenerator (
  152. * Algorithm algorithm)
  153. *
  154. * Functional Description:
  155. * This version of the constructor is used to create a random number
  156. * generator object that has been automatically seeded with the current
  157. * time. The algorithm specifies the algorithm to be used.
  158. *
  159. * Formal Parameters:
  160. * algorithm (i)
  161. * The random number generation algorithm to be used. The parameter
  162. * algorithm must be one of the following:
  163. *
  164. * ALGORITHM_RAN1
  165. * A good general purpose algorithm with a rather long period.
  166. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  167. * 29+ Kops (thousand operations per second).
  168. * ALGORITHM_RANQD2
  169. * A quick and dirty algorithm. Use this algorithm if speed is an
  170. * issue and the period of the random sequence is unimportant.
  171. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  172. * 49+ Kops (thousand operations per second).
  173. * ALGORITHM_RAN4
  174. * A slow algorithm with an exceptionally long period.
  175. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  176. * 18+ Kops (thousand operations per second).
  177. *
  178. * Return Value:
  179. * None.
  180. *
  181. * Side Effects:
  182. * None.
  183. *
  184. * Caveats:
  185. * None.
  186. */
  187. /*
  188. * RandomNumberGenerator (
  189. * Algorithm algorithm,
  190. * ULong seed)
  191. *
  192. * Functional Description:
  193. * This version of the constructor is used to create a random number
  194. * generator object which is seeded with the supplied value. The algorithm
  195. * specified the algorithm to be used.
  196. *
  197. * Formal Parameters:
  198. * algorithm (i)
  199. * The random number generation algorithm to be used. The parameter
  200. * algorithm must be one of the following:
  201. *
  202. * ALGORITHM_RAN1
  203. * A good general purpose algorithm with a rather long period.
  204. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  205. * 29+ Kops (thousand operations per second).
  206. * ALGORITHM_RANQD2
  207. * A quick and dirty algorithm. Use this algorithm if speed is an
  208. * issue and the period of the random sequence is unimportant.
  209. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  210. * 49+ Kops (thousand operations per second).
  211. * ALGORITHM_RAN4
  212. * A slow algorithm with an exceptionally long period.
  213. * This algorithm was benchmarked on a Gateway 2000 486/33C at
  214. * 18+ Kops (thousand operations per second).
  215. * seed (i)
  216. * A value used to seed the random number generator. If the seed value
  217. * is zero, the random number generator object will use a random seed
  218. * value based on the time.
  219. *
  220. * Return Value:
  221. * None.
  222. *
  223. * Side Effects:
  224. * None.
  225. *
  226. * Caveats:
  227. * None.
  228. */
  229. /*
  230. * ~RandomNumberGenerator ()
  231. *
  232. * Public
  233. *
  234. * Functional Description:
  235. * This is the destructor for the RandomNumberGenerator class.
  236. *
  237. * Formal Parameters:
  238. * None.
  239. *
  240. * Return Value:
  241. * None.
  242. *
  243. * Side Effects:
  244. * None.
  245. *
  246. * Caveats:
  247. * None.
  248. */
  249. /*
  250. * RandomValue GetRandomNumber (
  251. * RandomValue lo_extent,
  252. * RandomValue hi_extent)
  253. *
  254. * Public
  255. *
  256. * Functional Description:
  257. * This method is used to generate a random number between the
  258. * specified values.
  259. *
  260. * Formal Parameters:
  261. * lo_extent (i)
  262. * The lowest number you wish to receive.
  263. * hi_extent (i)
  264. * The highest number you wish to receive.
  265. *
  266. * Return Value:
  267. * A RandomValue in the range specified.
  268. *
  269. * Side Effects:
  270. * None.
  271. *
  272. * Caveats:
  273. * None.
  274. */
  275. /*
  276. * Void Reseed ()
  277. *
  278. * Public
  279. *
  280. * Functional Description:
  281. * This method is used to reseed the random number generator object using
  282. * the system time as the seed value.
  283. *
  284. * Formal Parameters:
  285. * None.
  286. *
  287. * Return Value:
  288. * None.
  289. *
  290. * Side Effects:
  291. * None.
  292. *
  293. * Caveats:
  294. * None.
  295. */
  296. /*
  297. * Void Reseed (
  298. * ULong seed)
  299. *
  300. * Public
  301. *
  302. * Functional Description:
  303. * This method is used to reseed the random number generator object using
  304. * the specified seed value.
  305. *
  306. * Formal Parameters:
  307. * seed (i)
  308. * A value used to seed the random number generator. If the seed value
  309. * is zero, the random number generator object will use a random seed
  310. * value based on the time.
  311. *
  312. * Return Value:
  313. * None.
  314. *
  315. * Side Effects:
  316. * None.
  317. *
  318. * Caveats:
  319. * None.
  320. */
  321. #endif // USE_RANDOM_CLASS
  322. #endif // _RANDOM_