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
2.1 KiB

  1. /***
  2. *rand.c - random number generator
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines rand(), srand() - random number generator
  8. *
  9. *Revision History:
  10. * 03-16-84 RN initial version
  11. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  12. * 05-31-88 PHG Merged DLL and normal versions
  13. * 06-06-89 JCR 386 mthread support
  14. * 03-15-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  15. * <cruntime.h> and fixed the copyright. Also, cleaned
  16. * up the formatting a bit.
  17. * 04-05-90 GJF Added #include <stdlib.h>.
  18. * 10-04-90 GJF New-style function declarators.
  19. * 07-17-91 GJF Multi-thread support for Win32 [_WIN32_].
  20. * 02-17-93 GJF Changed for new _getptd().
  21. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  22. * 09-06-94 CFW Remove Cruiser support.
  23. * 09-06-94 CFW Replace MTHREAD with _MT.
  24. *
  25. *******************************************************************************/
  26. #include <cruntime.h>
  27. #include <mtdll.h>
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30. #ifndef _MT
  31. static long holdrand = 1L;
  32. #endif
  33. /***
  34. *void srand(seed) - seed the random number generator
  35. *
  36. *Purpose:
  37. * Seeds the random number generator with the int given. Adapted from the
  38. * BASIC random number generator.
  39. *
  40. *Entry:
  41. * unsigned seed - seed to seed rand # generator with
  42. *
  43. *Exit:
  44. * None.
  45. *
  46. *Exceptions:
  47. *
  48. *******************************************************************************/
  49. void __cdecl srand (
  50. unsigned int seed
  51. )
  52. {
  53. #ifdef _MT
  54. _getptd()->_holdrand = (unsigned long)seed;
  55. #else
  56. holdrand = (long)seed;
  57. #endif
  58. }
  59. /***
  60. *int rand() - returns a random number
  61. *
  62. *Purpose:
  63. * returns a pseudo-random number 0 through 32767.
  64. *
  65. *Entry:
  66. * None.
  67. *
  68. *Exit:
  69. * Returns a pseudo-random number 0 through 32767.
  70. *
  71. *Exceptions:
  72. *
  73. *******************************************************************************/
  74. int __cdecl rand (
  75. void
  76. )
  77. {
  78. #ifdef _MT
  79. _ptiddata ptd = _getptd();
  80. return( ((ptd->_holdrand = ptd->_holdrand * 214013L
  81. + 2531011L) >> 16) & 0x7fff );
  82. #else
  83. return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
  84. #endif
  85. }