lrand48.c 3.6 KB

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