rand.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. rand.c
  9. Abstract:
  10. This module implements support for the random functions of the C library.
  11. Author:
  12. Evan Green 11-Jul-2013
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <stdlib.h>
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // Define constants used in the linear congruential generator.
  26. //
  27. #define RANDOM_MULTIPLIER 1103515245
  28. #define RANDOM_INCREMENT 12345
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. unsigned int ClRandomSeed = 1;
  39. //
  40. // ------------------------------------------------------------------ Functions
  41. //
  42. LIBC_API
  43. int
  44. rand (
  45. void
  46. )
  47. /*++
  48. Routine Description:
  49. This routine returns a pseudo-random number.
  50. Arguments:
  51. None.
  52. Return Value:
  53. Returns a pseudo-random integer between 0 and RAND_MAX, inclusive.
  54. --*/
  55. {
  56. return rand_r(&ClRandomSeed);
  57. }
  58. LIBC_API
  59. int
  60. rand_r (
  61. unsigned *Seed
  62. )
  63. /*++
  64. Routine Description:
  65. This routine implements the re-entrant and thread-safe version of the
  66. pseudo-random number generator.
  67. Arguments:
  68. Seed - Supplies a pointer to the seed to use. This seed will be updated
  69. to contain the next seed.
  70. Return Value:
  71. Returns a pseudo-random integer between 0 and RAND_MAX, inclusive.
  72. --*/
  73. {
  74. *Seed = (*Seed * RANDOM_MULTIPLIER) + RANDOM_INCREMENT;
  75. return *Seed % ((unsigned int)RAND_MAX + 1);
  76. }
  77. LIBC_API
  78. void
  79. srand (
  80. unsigned Seed
  81. )
  82. /*++
  83. Routine Description:
  84. This routine sets the seed for the rand function.
  85. Arguments:
  86. Seed - Supplies the seed to use.
  87. Return Value:
  88. None.
  89. --*/
  90. {
  91. ClRandomSeed = Seed;
  92. return;
  93. }
  94. //
  95. // --------------------------------------------------------- Internal Functions
  96. //