lrand48.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  61. char *grass_random_seed = getenv("GRASS_RANDOM_SEED");
  62. if(!grass_random_seed) grass_random_seed = getenv("SOURCE_DATE_EPOCH");
  63. if(grass_random_seed) {
  64. seed = strtoull(grass_random_seed, NULL, 10);
  65. } else {
  66. seed = (unsigned long) getpid();
  67. #ifdef HAVE_GETTIMEOFDAY
  68. {
  69. struct timeval tv;
  70. if (gettimeofday(&tv, NULL) < 0)
  71. G_fatal_error(_("gettimeofday failed: %s"), strerror(errno));
  72. seed += (unsigned long) tv.tv_sec;
  73. seed += (unsigned long) tv.tv_usec;
  74. }
  75. #else
  76. {
  77. time_t t = time(NULL);
  78. seed += (unsigned long) t;
  79. }
  80. #endif
  81. }
  82. G_srand48((long) seed);
  83. return (long) seed;
  84. }
  85. static void G__next(void)
  86. {
  87. uint32 a0x0 = a0 * x0;
  88. uint32 a0x1 = a0 * x1;
  89. uint32 a0x2 = a0 * x2;
  90. uint32 a1x0 = a1 * x0;
  91. uint32 a1x1 = a1 * x1;
  92. uint32 a2x0 = a2 * x0;
  93. uint32 y0 = LO(a0x0) + b0;
  94. uint32 y1 = LO(a0x1) + LO(a1x0) + HI(a0x0);
  95. uint32 y2 = LO(a0x2) + LO(a1x1) + LO(a2x0) + HI(a0x1) + HI(a1x0);
  96. if (!seeded)
  97. G_fatal_error(_("Pseudo-random number generator not seeded"));
  98. x0 = (uint16) LO(y0);
  99. y1 += HI(y0);
  100. x1 = (uint16) LO(y1);
  101. y2 += HI(y1);
  102. x2 = (uint16) LO(y2);
  103. }
  104. /*!
  105. * \brief Generate an integer in the range [0, 2^31)
  106. *
  107. * \return the generated value
  108. */
  109. long G_lrand48(void)
  110. {
  111. uint32 r;
  112. G__next();
  113. r = ((uint32) x2 << 15) | ((uint32) x1 >> 1);
  114. return (long) r;
  115. }
  116. /*!
  117. * \brief Generate an integer in the range [-2^31, 2^31)
  118. *
  119. * \return the generated value
  120. */
  121. long G_mrand48(void)
  122. {
  123. uint32 r;
  124. G__next();
  125. r = ((uint32) x2 << 16) | ((uint32) x1);
  126. return (long) (int32) r;
  127. }
  128. /*!
  129. * \brief Generate a floating-point value in the range [0,1)
  130. *
  131. * \return the generated value
  132. */
  133. double G_drand48(void)
  134. {
  135. double r = 0.0;
  136. G__next();
  137. r += x2;
  138. r *= 0x10000;
  139. r += x1;
  140. r *= 0x10000;
  141. r += x0;
  142. r /= 281474976710656.0; /* 2^48 */
  143. return r;
  144. }
  145. /*
  146. Test program
  147. int main(int argc, char **argv)
  148. {
  149. long s = (argc > 1) ? atol(argv[1]) : 0;
  150. int i;
  151. srand48(s);
  152. G_srand48(s);
  153. for (i = 0; i < 100; i++) {
  154. printf("%.50f %.50f\n", drand48(), G_drand48());
  155. printf("%lu %lu\n", lrand48(), G_lrand48());
  156. printf("%ld %ld\n", mrand48(), G_mrand48());
  157. }
  158. return 0;
  159. }
  160. */