lrand48.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*!
  2. * \file lib/gis/lrand48.c
  3. *
  4. * \brief GIS Library - Pseudo-random number generation
  5. *
  6. * (C) 2014 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Glynn Clements
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. #ifdef HAVE_GETTIMEOFDAY
  20. #include <sys/time.h>
  21. #else
  22. #include <time.h>
  23. #endif
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. typedef unsigned short uint16;
  27. typedef unsigned int uint32;
  28. typedef signed int int32;
  29. static uint16 x0, x1, x2;
  30. static const uint32 a0 = 0xE66D;
  31. static const uint32 a1 = 0xDEEC;
  32. static const uint32 a2 = 0x5;
  33. static const uint32 b0 = 0xB;
  34. static int seeded;
  35. #define LO(x) ((x) & 0xFFFFU)
  36. #define HI(x) ((x) >> 16)
  37. /*!
  38. * \brief Seed the pseudo-random number generator
  39. *
  40. * \param seedval 32-bit integer used to seed the PRNG
  41. */
  42. void G_srand48(long seedval)
  43. {
  44. uint32 x = (uint32) *(unsigned long *)&seedval;
  45. x2 = (uint16) HI(x);
  46. x1 = (uint16) LO(x);
  47. x0 = (uint16) 0x330E;
  48. seeded = 1;
  49. }
  50. /*!
  51. * \brief Seed the pseudo-random number generator from the time and PID
  52. *
  53. * A weak hash of the current time and PID is generated and used to
  54. * seed the PRNG
  55. *
  56. * \return generated seed value passed to G_srand48()
  57. */
  58. long G_srand48_auto(void)
  59. {
  60. unsigned long seed = (unsigned long) getpid();
  61. #ifdef HAVE_GETTIMEOFDAY
  62. {
  63. struct timeval tv;
  64. if (gettimeofday(&tv, NULL) < 0)
  65. G_fatal_error(_("gettimeofday failed: %s"), strerror(errno));
  66. seed += (unsigned long) tv.tv_sec;
  67. seed += (unsigned long) tv.tv_usec;
  68. }
  69. #else
  70. {
  71. time_t t = time(NULL);
  72. seed += (unsigned long) t;
  73. }
  74. #endif
  75. G_srand48((long) seed);
  76. return (long) seed;
  77. }
  78. static void G__next(void)
  79. {
  80. uint32 a0x0 = a0 * x0;
  81. uint32 a0x1 = a0 * x1;
  82. uint32 a0x2 = a0 * x2;
  83. uint32 a1x0 = a1 * x0;
  84. uint32 a1x1 = a1 * x1;
  85. uint32 a2x0 = a2 * x0;
  86. uint32 y0 = LO(a0x0) + b0;
  87. uint32 y1 = LO(a0x1) + LO(a1x0) + HI(a0x0);
  88. uint32 y2 = LO(a0x2) + LO(a1x1) + LO(a2x0) + HI(a0x1) + HI(a1x0);
  89. if (!seeded)
  90. G_fatal_error(_("Pseudo-random number generator not seeded"));
  91. x0 = (uint16) LO(y0);
  92. y1 += HI(y0);
  93. x1 = (uint16) LO(y1);
  94. y2 += HI(y1);
  95. x2 = (uint16) LO(y2);
  96. }
  97. /*!
  98. * \brief Generate an integer in the range [0, 2^31)
  99. *
  100. * \return the generated value
  101. */
  102. long G_lrand48(void)
  103. {
  104. uint32 r;
  105. G__next();
  106. r = ((uint32) x2 << 15) | ((uint32) x1 >> 1);
  107. return (long) r;
  108. }
  109. /*!
  110. * \brief Generate an integer in the range [-2^31, 2^31)
  111. *
  112. * \return the generated value
  113. */
  114. long G_mrand48(void)
  115. {
  116. uint32 r;
  117. G__next();
  118. r = ((uint32) x2 << 16) | ((uint32) x1);
  119. return (long) (int32) r;
  120. }
  121. /*!
  122. * \brief Generate a floating-point value in the range [0,1)
  123. *
  124. * \return the generated value
  125. */
  126. double G_drand48(void)
  127. {
  128. double r = 0.0;
  129. G__next();
  130. r += x2;
  131. r *= 0x10000;
  132. r += x1;
  133. r *= 0x10000;
  134. r += x0;
  135. r /= 281474976710656.0; /* 2^48 */
  136. return r;
  137. }
  138. /*
  139. Test program
  140. int main(int argc, char **argv)
  141. {
  142. long s = (argc > 1) ? atol(argv[1]) : 0;
  143. int i;
  144. srand48(s);
  145. G_srand48(s);
  146. for (i = 0; i < 100; i++) {
  147. printf("%.50f %.50f\n", drand48(), G_drand48());
  148. printf("%lu %lu\n", lrand48(), G_lrand48());
  149. printf("%ld %ld\n", mrand48(), G_mrand48());
  150. }
  151. return 0;
  152. }
  153. */