Leaked source code of windows server 2003
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.

97 lines
3.7 KiB

  1. /*
  2. * FILE: RandGen.h
  3. *
  4. * PURPOSE: Header file for a random number generator, based on
  5. * original code by George Marsaglia.
  6. *
  7. * AUTHOR: Karl Denninghoff, November 1998
  8. *
  9. * Copyright: Microsoft Corporation, 1998
  10. */
  11. #ifndef RANDGEN_H
  12. #define RANDGEN_H
  13. #ifndef _BASETSD_H_ // if the base sized types not defined (header file not included)
  14. typedef unsigned __int32 UINT32 ;
  15. #endif
  16. typedef unsigned __int16 UINT16 ;
  17. // maximum number returned by RandInt()
  18. const UINT32 MAX_RANDGEN = 0xffffffffL;
  19. //-------------------------------------------------------------
  20. //
  21. // CLASS CSARandGen
  22. //
  23. // PURPOSE: Generates random numbers of 32 bits with a period of
  24. // about 2^250. However, since the seed is 32 bits, there
  25. // are only 2^32 different possible sequences accessible
  26. // through the interface provided.
  27. //
  28. // USAGE RULES and REMARKS:
  29. // 1) You may have as many instances (random number generators)
  30. // as you wish simultneously in a process.
  31. // 2) The code is not thread-safe, i.e., you must not make
  32. // simultaneous calls to a particular random number generator
  33. // (CSARandGen instance) on different threads. If more than
  34. // one thread is to call a particular generator, you should
  35. // provide the synchronization necessary in some wrapper class.
  36. // 3) Generation and subsequent conversion to a random real
  37. // uniformly distributed over the interval [0,1] is provided
  38. // by the RandReal() member function.
  39. // 4) If you do not explicitly seed the generator, then the sequence
  40. // generated will with high probability be indestinguishable from
  41. // sequences generated by another instance. It will be an
  42. // unrelated sequence that you will not be able to re-generate.
  43. // 5) The constructor seeds the generator automatically using
  44. // system clock input. However, it does not tell you this value.
  45. // If you want a random seed (one you likely have not used before) and
  46. // you also want to be able to repeat the sequence later, then
  47. // the following technique will work well.
  48. //
  49. // CSARandGen *pRandGen = new CSARandGen;
  50. // UINT32 ulSeed = pRandGen->RandInt(); // gets a random seed value
  51. // pRandGen->RandSeed( ulSeed );
  52. // // now save ulSeed so you can use it again to seed
  53. // // a generator. Any CSARandGen instance will produce
  54. // // the same sequence after calling the seed function
  55. // // with the same seed value.
  56. //
  57. //
  58. class CSARandGen {
  59. public: // public functions
  60. _stdcall CSARandGen();
  61. // seeds the random number generator, can be called at
  62. // any time to re-seed, seeding with the same number starts
  63. // the same sequence.
  64. void _stdcall RandSeed( UINT32 ulSeed );
  65. // returns uniformly distributed random number
  66. // between 0 and MAX_RANDGEN inclusive, period about 2^250
  67. UINT32 _stdcall RandInt();
  68. // returns uniformly distributed random floating point number
  69. // between 0 and 1. Two adjacent possible numbers are
  70. // 1/MAX_RANDGEN apart.
  71. double _stdcall RandReal()
  72. {
  73. return double(RandInt())/double(MAX_RANDGEN);
  74. };
  75. private: // private data members
  76. UINT16 mother1[10];
  77. UINT16 mother2[10];
  78. static LONG slCInstances; // keeps count of instantiations, used to ensure
  79. // uniqueness of default seeds when two instantiations
  80. // result in the same tick and filetime.
  81. };
  82. #endif