GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
lrand48.c
Go to the documentation of this file.
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 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 #ifdef HAVE_GETTIMEOFDAY
23 #include <sys/time.h>
24 #else
25 #include <time.h>
26 #endif
27 
28 #include <sys/types.h>
29 #include <unistd.h>
30 
31 typedef unsigned short uint16;
32 typedef unsigned int uint32;
33 typedef signed int int32;
34 
35 static uint16 x0, x1, x2;
36 static const uint32 a0 = 0xE66D;
37 static const uint32 a1 = 0xDEEC;
38 static const uint32 a2 = 0x5;
39 
40 static const uint32 b0 = 0xB;
41 
42 static int seeded;
43 
44 #define LO(x) ((x) & 0xFFFFU)
45 #define HI(x) ((x) >> 16)
46 
47 /*!
48  * \brief Seed the pseudo-random number generator
49  *
50  * \param seedval 32-bit integer used to seed the PRNG
51  */
52 
53 void G_srand48(long seedval)
54 {
55  uint32 x = (uint32) *(unsigned long *)&seedval;
56  x2 = (uint16) HI(x);
57  x1 = (uint16) LO(x);
58  x0 = (uint16) 0x330E;
59  seeded = 1;
60 }
61 
62 /*!
63  * \brief Seed the pseudo-random number generator from the time and PID
64  *
65  * A weak hash of the current time and PID is generated and used to
66  * seed the PRNG
67  *
68  * \return generated seed value passed to G_srand48()
69  */
70 
71 long G_srand48_auto(void)
72 {
73  unsigned long seed;
74  char *grass_random_seed = getenv("GRASS_RANDOM_SEED");
75  if(!grass_random_seed) grass_random_seed = getenv("SOURCE_DATE_EPOCH");
76  if(grass_random_seed) {
77  seed = strtoull(grass_random_seed, NULL, 10);
78  } else {
79  seed = (unsigned long) getpid();
80 
81 #ifdef HAVE_GETTIMEOFDAY
82  {
83  struct timeval tv;
84  if (gettimeofday(&tv, NULL) < 0)
85  G_fatal_error(_("gettimeofday failed: %s"), strerror(errno));
86  seed += (unsigned long) tv.tv_sec;
87  seed += (unsigned long) tv.tv_usec;
88  }
89 #else
90  {
91  time_t t = time(NULL);
92  seed += (unsigned long) t;
93  }
94 #endif
95  }
96 
97  G_srand48((long) seed);
98  return (long) seed;
99 }
100 
101 static void G__next(void)
102 {
103  uint32 a0x0 = a0 * x0;
104  uint32 a0x1 = a0 * x1;
105  uint32 a0x2 = a0 * x2;
106  uint32 a1x0 = a1 * x0;
107  uint32 a1x1 = a1 * x1;
108  uint32 a2x0 = a2 * x0;
109 
110  uint32 y0 = LO(a0x0) + b0;
111  uint32 y1 = LO(a0x1) + LO(a1x0) + HI(a0x0);
112  uint32 y2 = LO(a0x2) + LO(a1x1) + LO(a2x0) + HI(a0x1) + HI(a1x0);
113 
114  if (!seeded)
115  G_fatal_error(_("Pseudo-random number generator not seeded"));
116 
117  x0 = (uint16) LO(y0);
118  y1 += HI(y0);
119  x1 = (uint16) LO(y1);
120  y2 += HI(y1);
121  x2 = (uint16) LO(y2);
122 }
123 
124 /*!
125  * \brief Generate an integer in the range [0, 2^31)
126  *
127  * \return the generated value
128  */
129 
130 long G_lrand48(void)
131 {
132  uint32 r;
133  G__next();
134  r = ((uint32) x2 << 15) | ((uint32) x1 >> 1);
135  return (long) r;
136 }
137 
138 /*!
139  * \brief Generate an integer in the range [-2^31, 2^31)
140  *
141  * \return the generated value
142  */
143 
144 long G_mrand48(void)
145 {
146  uint32 r;
147  G__next();
148  r = ((uint32) x2 << 16) | ((uint32) x1);
149  return (long) (int32) r;
150 }
151 
152 /*!
153  * \brief Generate a floating-point value in the range [0,1)
154  *
155  * \return the generated value
156  */
157 
158 double G_drand48(void)
159 {
160  double r = 0.0;
161  G__next();
162  r += x2;
163  r *= 0x10000;
164  r += x1;
165  r *= 0x10000;
166  r += x0;
167  r /= 281474976710656.0; /* 2^48 */
168  return r;
169 }
170 
171 /*
172 
173 Test program
174 
175 int main(int argc, char **argv)
176 {
177  long s = (argc > 1) ? atol(argv[1]) : 0;
178  int i;
179 
180  srand48(s);
181  G_srand48(s);
182 
183  for (i = 0; i < 100; i++) {
184  printf("%.50f %.50f\n", drand48(), G_drand48());
185  printf("%lu %lu\n", lrand48(), G_lrand48());
186  printf("%ld %ld\n", mrand48(), G_mrand48());
187  }
188 
189  return 0;
190 }
191 
192 */
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_srand48(long seedval)
Seed the pseudo-random number generator.
Definition: lrand48.c:53
long G_mrand48(void)
Generate an integer in the range [-2^31, 2^31)
Definition: lrand48.c:144
#define NULL
Definition: ccmath.h:32
#define x
unsigned short uint16
Definition: lrand48.c:31
unsigned int uint32
Definition: lrand48.c:32
double t
Definition: r_raster.c:39
long G_lrand48(void)
Generate an integer in the range [0, 2^31)
Definition: lrand48.c:130
#define LO(x)
Definition: lrand48.c:44
long G_srand48_auto(void)
Seed the pseudo-random number generator from the time and PID.
Definition: lrand48.c:71
#define _(str)
Definition: glocale.h:10
signed int int32
Definition: lrand48.c:33
double G_drand48(void)
Generate a floating-point value in the range [0,1)
Definition: lrand48.c:158
char * getenv()
#define HI(x)
Definition: lrand48.c:45
double r
Definition: r_raster.c:39