[GRASS-SVN] r61471 - in grass/branches/releasebranch_7_0: . imagery/i.ortho.photo/i.ortho.rectify imagery/i.rectify include/defs lib/gis lib/gmath lib/raster raster/r.flow raster/r.li/r.li.daemon raster/r.proj raster/r.random raster/r.spread raster/r.surf.fractal raster/r.surf.gauss raster/r.surf.random vector/v.extract vector/v.kcv vector/v.qcount vector/v.random

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 31 04:25:00 PDT 2014


Author: neteler
Date: 2014-07-31 04:25:00 -0700 (Thu, 31 Jul 2014)
New Revision: 61471

Added:
   grass/branches/releasebranch_7_0/lib/gis/lrand48.c
Modified:
   grass/branches/releasebranch_7_0/
   grass/branches/releasebranch_7_0/imagery/i.ortho.photo/i.ortho.rectify/readcell.c
   grass/branches/releasebranch_7_0/imagery/i.rectify/readcell.c
   grass/branches/releasebranch_7_0/include/defs/gis.h
   grass/branches/releasebranch_7_0/include/defs/gmath.h
   grass/branches/releasebranch_7_0/lib/gmath/gauss.c
   grass/branches/releasebranch_7_0/lib/gmath/rand1.c
   grass/branches/releasebranch_7_0/lib/raster/color_rand.c
   grass/branches/releasebranch_7_0/raster/r.flow/main.c
   grass/branches/releasebranch_7_0/raster/r.li/r.li.daemon/daemon.c
   grass/branches/releasebranch_7_0/raster/r.proj/readcell.c
   grass/branches/releasebranch_7_0/raster/r.random/creat_rand.c
   grass/branches/releasebranch_7_0/raster/r.spread/main.c
   grass/branches/releasebranch_7_0/raster/r.spread/pick_dist.c
   grass/branches/releasebranch_7_0/raster/r.spread/pick_ignite.c
   grass/branches/releasebranch_7_0/raster/r.surf.fractal/spec_syn.c
   grass/branches/releasebranch_7_0/raster/r.surf.gauss/gaussurf.c
   grass/branches/releasebranch_7_0/raster/r.surf.random/randsurf.c
   grass/branches/releasebranch_7_0/vector/v.extract/main.c
   grass/branches/releasebranch_7_0/vector/v.kcv/main.c
   grass/branches/releasebranch_7_0/vector/v.qcount/findquads.c
   grass/branches/releasebranch_7_0/vector/v.random/main.c
Log:
RNG backport: Add portable lrand48 (etc) implementation (r61350); Generate fatal error if requesting values without having seeded (r61352); Replace rand/random/lrand48/etc with lib/gis random number functions (r61415); Use lib/gis PRNG for readcell() (r61416)


Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Deleted: svn:mergeinfo
   - /grass/trunk:60590,61447

Modified: grass/branches/releasebranch_7_0/imagery/i.ortho.photo/i.ortho.rectify/readcell.c
===================================================================
--- grass/branches/releasebranch_7_0/imagery/i.ortho.photo/i.ortho.rectify/readcell.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/imagery/i.ortho.photo/i.ortho.rectify/readcell.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -29,6 +29,8 @@
     else
 	select_current_env();
 
+    G_srand48(0);
+
     nrows = Rast_window_rows();
     ncols = Rast_window_cols();
 
@@ -119,7 +121,7 @@
 
 block *get_block(struct cache * c, int idx)
 {
-    int replace = rand() % c->nblocks;
+    int replace = G_lrand48() % c->nblocks;
     block *p = &c->blocks[replace];
     int ref = c->refs[replace];
     off_t offset = (off_t) idx * sizeof(DCELL) << L2BSIZE;

Modified: grass/branches/releasebranch_7_0/imagery/i.rectify/readcell.c
===================================================================
--- grass/branches/releasebranch_7_0/imagery/i.rectify/readcell.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/imagery/i.rectify/readcell.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -27,6 +27,8 @@
     int nblocks;
     int i;
 
+    G_srand48(0);
+
     nrows = Rast_input_window_rows();
     ncols = Rast_input_window_cols();
 
@@ -106,7 +108,7 @@
 
 block *get_block(struct cache * c, int idx)
 {
-    int replace = rand() % c->nblocks;
+    int replace = G_lrand48() % c->nblocks;
     block *p = &c->blocks[replace];
     int ref = c->refs[replace];
     off_t offset = (off_t) idx * sizeof(DCELL) << L2BSIZE;

Modified: grass/branches/releasebranch_7_0/include/defs/gis.h
===================================================================
--- grass/branches/releasebranch_7_0/include/defs/gis.h	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/include/defs/gis.h	2014-07-31 11:25:00 UTC (rev 61471)
@@ -391,6 +391,13 @@
 const char *G_location(void);
 char *G_location_path(void);
 
+/* lrand48.c */
+void G_srand48(long);
+long G_srand48_auto(void);
+long G_lrand48(void);
+long G_mrand48(void);
+double G_drand48(void);
+
 /* ls.c */
 void G_set_ls_filter(int (*)(const char *, void *), void *);
 void G_set_ls_exclude_filter(int (*)(const char *, void *), void *);

Modified: grass/branches/releasebranch_7_0/include/defs/gmath.h
===================================================================
--- grass/branches/releasebranch_7_0/include/defs/gmath.h	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/include/defs/gmath.h	2014-07-31 11:25:00 UTC (rev 61471)
@@ -22,14 +22,16 @@
 extern int fft2(int, double (*)[2], int, int, int);
 
 /* gauss.c */
-extern double G_math_rand_gauss(int, double);
+extern double G_math_rand_gauss(double);
 
 /* max_pow2.c */
 extern long G_math_max_pow2 (long n);
 extern long G_math_min_pow2 (long n);
 
 /* rand1.c */
-extern float G_math_rand(int);
+extern void G_math_srand(int);
+extern int G_math_srand_auto(void);
+extern float G_math_rand(void);
 
 /* del2g.c */
 extern int del2g(double *[2], int, double);

Copied: grass/branches/releasebranch_7_0/lib/gis/lrand48.c (from rev 61350, grass/trunk/lib/gis/lrand48.c)
===================================================================
--- grass/branches/releasebranch_7_0/lib/gis/lrand48.c	                        (rev 0)
+++ grass/branches/releasebranch_7_0/lib/gis/lrand48.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -0,0 +1,185 @@
+/*!
+ * \file lib/gis/lrand48.c
+ *
+ * \brief GIS Library - Pseudo-random number generation
+ *
+ * (C) 2014 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public License
+ * (>=v2). Read the file COPYING that comes with GRASS for details.
+ *
+ * \author Glynn Clements
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#ifdef HAVE_GETTIMEOFDAY
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+
+#include <sys/types.h>
+#include <unistd.h>
+
+typedef unsigned short uint16;
+typedef unsigned int uint32;
+typedef signed int int32;
+
+static uint16 x0, x1, x2;
+static const uint32 a0 = 0xE66D;
+static const uint32 a1 = 0xDEEC;
+static const uint32 a2 = 0x5;
+
+static const uint32 b0 = 0xB;
+
+static int seeded;
+
+#define LO(x) ((x) & 0xFFFFU)
+#define HI(x) ((x) >> 16)
+
+/*!
+ * \brief Seed the pseudo-random number generator
+ *
+ * \param seedval 32-bit integer used to seed the PRNG
+ */
+
+void G_srand48(long seedval)
+{
+    uint32 x = (uint32) *(unsigned long *)&seedval;
+    x2 = (uint16) HI(x);
+    x1 = (uint16) LO(x);
+    x0 = (uint16) 0x330E;
+    seeded = 1;
+}
+
+/*!
+ * \brief Seed the pseudo-random number generator from the time and PID
+ *
+ * A weak hash of the current time and PID is generated and used to
+ * seed the PRNG
+ *
+ * \return generated seed value passed to G_srand48()
+ */
+
+long G_srand48_auto(void)
+{
+    unsigned long seed = (unsigned long) getpid();
+
+#ifdef HAVE_GETTIMEOFDAY
+    {
+	struct timeval tv;
+	if (gettimeofday(&tv, NULL) < 0)
+	    G_fatal_error(_("gettimeofday failed: %s"), strerror(errno));
+	seed += (unsigned long) tv.tv_sec;
+	seed += (unsigned long) tv.tv_usec;
+    }
+#else
+    {
+	time_t t = time(NULL);
+	seed += (unsigned long) t;
+    }
+#endif
+
+    G_srand48((long) seed);
+    return (long) seed;
+}
+
+static void G__next(void)
+{
+    uint32 a0x0 = a0 * x0;
+    uint32 a0x1 = a0 * x1;
+    uint32 a0x2 = a0 * x2;
+    uint32 a1x0 = a1 * x0;
+    uint32 a1x1 = a1 * x1;
+    uint32 a2x0 = a2 * x0;
+
+    uint32 y0 = LO(a0x0) + b0;
+    uint32 y1 = LO(a0x1) + LO(a1x0) + HI(a0x0);
+    uint32 y2 = LO(a0x2) + LO(a1x1) + LO(a2x0) + HI(a0x1) + HI(a1x0);
+
+    if (!seeded)
+	G_fatal_error(_("Pseudo-random number generator not seeded"));
+
+    x0 = (uint16) LO(y0);
+    y1 += HI(y0);
+    x1 = (uint16) LO(y1);
+    y2 += HI(y1);
+    x2 = (uint16) LO(y2);
+}
+
+/*!
+ * \brief Generate an integer in the range [0, 2^31)
+ *
+ * \return the generated value
+ */
+
+long G_lrand48(void)
+{
+    uint32 r;
+    G__next();
+    r = ((uint32) x2 << 15) | ((uint32) x1 >> 1);
+    return (long) r;
+}
+
+/*!
+ * \brief Generate an integer in the range [-2^31, 2^31)
+ *
+ * \return the generated value
+ */
+
+long G_mrand48(void)
+{
+    uint32 r;
+    G__next();
+    r = ((uint32) x2 << 16) | ((uint32) x1);
+    return (long) (int32) r;
+}
+
+/*!
+ * \brief Generate a floating-point value in the range [0,1)
+ *
+ * \return the generated value
+ */
+
+double G_drand48(void)
+{
+    double r = 0.0;
+    G__next();
+    r += x2;
+    r *= 0x10000;
+    r += x1;
+    r *= 0x10000;
+    r += x0;
+    r /= 281474976710656.0; /* 2^48 */
+    return r;
+}
+
+/*
+
+Test program
+
+int main(int argc, char **argv)
+{
+    long s = (argc > 1) ? atol(argv[1]) : 0;
+    int i;
+
+    srand48(s);
+    G_srand48(s);
+
+    for (i = 0; i < 100; i++) {
+	printf("%.50f %.50f\n", drand48(), G_drand48());
+	printf("%lu %lu\n", lrand48(), G_lrand48());
+	printf("%ld %ld\n", mrand48(), G_mrand48());
+    }
+
+    return 0;
+}
+
+*/

Modified: grass/branches/releasebranch_7_0/lib/gmath/gauss.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/gmath/gauss.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/lib/gmath/gauss.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -14,14 +14,14 @@
  * \return double
  */
 
-double G_math_rand_gauss(const int seed, const double sigma)
+double G_math_rand_gauss(double sigma)
 {
     double x, y, r2;
 
     do {
 	/* choose x,y in uniform square (-1,-1) to (+1,+1) */
-	x = -1 + 2 * G_math_rand(seed);
-	y = -1 + 2 * G_math_rand(seed);
+	x = -1 + 2 * G_math_rand();
+	y = -1 + 2 * G_math_rand();
 
 	/* see if it is in the unit circle */
 	r2 = x * x + y * y;

Modified: grass/branches/releasebranch_7_0/lib/gmath/rand1.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/gmath/rand1.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/lib/gmath/rand1.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -1,5 +1,5 @@
-#include <grass/config.h>
 #include <stdlib.h>
+#include <grass/gis.h>
 #include <grass/gmath.h>
 
 
@@ -14,17 +14,29 @@
  * \return float
  */
 
-float G_math_rand(int seed)
+float G_math_rand(void)
 {
-#if defined(HAVE_DRAND48)
-    if (seed < 0)
-	srand48(-seed);
+    return G_drand48();
+}
 
-    return (float)drand48();
-#else
-    if (seed < 0)
-	srand(-seed);
+/*!
+ * \brief Seed the pseudo-random number generator
+ *
+ * \param seedval 32-bit integer used to seed the PRNG
+ */
 
-    return 1.0f * rand() / RAND_MAX;
-#endif
+void G_math_srand(int seed)
+{
+    G_srand48(seed);
 }
+
+/*!
+ * \brief Seed the pseudo-random number generator from the time and PID
+ *
+ * \return generated seed value passed to G_srand48()
+ */
+
+int G_math_srand_auto(void)
+{
+    return (int) G_srand48_auto();
+}

Modified: grass/branches/releasebranch_7_0/lib/raster/color_rand.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/raster/color_rand.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/lib/raster/color_rand.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -33,16 +33,17 @@
 	G_fatal_error(_("Rast_make_random_colors: min (%d) > max (%d)"),
 		      min, max);
 
-    srand(time(NULL));
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 
-    count = MAX_COLORS - DEVIATION + rand() % DEVIATION;
+    count = MAX_COLORS - DEVIATION + G_lrand48() % DEVIATION;
     if (count > max - min + 1)
 	count = max - min + 1;
 
     for (n = 1; n <= count; n++) {
-	red = rand() & 0377;
-	grn = rand() & 0377;
-	blu = rand() & 0377;
+	red = G_lrand48() & 0xff;
+	grn = G_lrand48() & 0xff;
+	blu = G_lrand48() & 0xff;
 	Rast_add_modular_c_color_rule(&n, red, grn, blu,
 				      &n, red, grn, blu, colors);
     }

Modified: grass/branches/releasebranch_7_0/raster/r.flow/main.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.flow/main.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.flow/main.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -26,8 +26,6 @@
  **  Update MN: commented line 387
  */
 
-#include <stdlib.h>		/* for the random number generation */
-#include <time.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/glocale.h>
@@ -320,7 +318,8 @@
 
     ystep = region.ns_res * (double)loopstep;
 
-    srand(time(0));
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 
     for (row = 0, y = (double)region.north - (region.ns_res * .5);
 	 row < region.rows; row += loopstep, y -= ystep) {
@@ -337,9 +336,9 @@
 #ifdef OFFSET
 		/* disabled by helena June 2005 */
 		roffset = parm.offset * (double)region.ew_res
-		    * ((2. * (double)rand() / (double)RAND_MAX) - 1.);
+		    * ((2. * G_drand48()) - 1.);
 		coffset = parm.offset * (double)region.ns_res
-		    * ((2. * (double)rand() / (double)RAND_MAX) - 1.);
+		    * ((2. * G_drand48()) - 1.);
 #endif
 		pts.x = x;
 		pts.y = y;

Modified: grass/branches/releasebranch_7_0/raster/r.li/r.li.daemon/daemon.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.li/r.li.daemon/daemon.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.li/r.li.daemon/daemon.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -15,7 +15,6 @@
  * \include
  * 
  */
-#include <grass/config.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <fcntl.h>
@@ -36,11 +35,6 @@
 #include <grass/glocale.h>
 #include "daemon.h"
 
-#ifdef __MINGW32__
-#define srandom srand
-#define random rand
-#endif
-
 int calculateIndex(char *file, rli_func *f,
 		   char **parameters, char *raster, char *output)
 {
@@ -524,11 +518,11 @@
 	    G_fatal_error(_("Too many units to place"));
 	assigned = G_malloc(units * sizeof(int));
 	i = 0;
-	srandom(0);
+	G_srand48(0);
 	while (i < units) {
 	    int j, position, found = FALSE;
 
-	    position = random() % max_units;
+	    position = G_lrand48() % max_units;
 	    for (j = 0; j < i; j++) {
 		if (assigned[j] == position)
 		    found = TRUE;
@@ -592,7 +586,7 @@
 	if (r_strat_len < g->rl || c_strat_len < g->cl)
 	    G_fatal_error(_("Too many stratified random sample for raster map"));
 	loop = r_strat * c_strat;
-	srandom(0);
+	G_srand48(0);
 	for (i = 0; i < loop; i++) {
 	    msg m;
 
@@ -600,9 +594,9 @@
 		m.type = AREA;
 		m.f.f_a.aid = i;
 		m.f.f_a.x = (int)g->sf_x + ((i % c_strat) * c_strat_len) +
-		    (random() % (c_strat_len - g->cl));
+		    (G_lrand48() % (c_strat_len - g->cl));
 		m.f.f_a.y = (int)g->sf_y + (rint(i / c_strat) * r_strat_len) +
-		    (random() % (r_strat_len - g->rl));
+		    (G_lrand48() % (r_strat_len - g->rl));
 		m.f.f_a.rl = g->rl;
 		m.f.f_a.cl = g->cl;
 		insertNode(l, m);
@@ -611,10 +605,10 @@
 		m.type = MASKEDAREA;
 		m.f.f_ma.aid = i;
 		m.f.f_ma.x = (int)g->sf_x + ((i % c_strat) * c_strat_len) +
-		    (random() % (c_strat_len - g->cl));
+		    (G_lrand48() % (c_strat_len - g->cl));
 		m.f.f_ma.y =
 		    (int)g->sf_y + (rint(i / c_strat) * r_strat_len) +
-		    (random() % (r_strat_len - g->rl));
+		    (G_lrand48() % (r_strat_len - g->rl));
 		m.f.f_ma.rl = g->rl;
 		m.f.f_ma.cl = g->cl;
 		strcpy(m.f.f_ma.mask, g->maskname);

Modified: grass/branches/releasebranch_7_0/raster/r.proj/readcell.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.proj/readcell.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.proj/readcell.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -26,6 +26,8 @@
     int nblocks;
     int i;
 
+    G_srand48(0);
+
     nrows = Rast_input_window_rows();
     ncols = Rast_input_window_cols();
 
@@ -114,7 +116,7 @@
 block *get_block(struct cache * c, int idx)
 {
     int fd;
-    int replace = rand() % c->nblocks;
+    int replace = G_lrand48() % c->nblocks;
     block *p = &c->blocks[replace];
     int ref = c->refs[replace];
     off_t offset = (off_t) idx * sizeof(FCELL) << L2BSIZE;

Modified: grass/branches/releasebranch_7_0/raster/r.random/creat_rand.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.random/creat_rand.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.random/creat_rand.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -1,43 +1,14 @@
-#include <time.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <grass/config.h>
+#include <grass/gis.h>
 
 
-#ifndef USE_RAND
-
-#ifndef HAVE_DRAND48
-#define lrand48() (((long) rand() ^ ((long) rand() << 16)) & 0x7FFFFFFF)
-#define srand48(sv) (srand((unsigned)(sv)))
-#endif
-
-
 long make_rand(void)
 {
-    return lrand48();
+    return G_lrand48();
 }
 
 void init_rand(void)
 {
-    srand48((long)time((time_t *) 0));
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 }
 
-#else
-
-static long labs(int n)
-{
-    return n < 0 ? (-n) : n;
-}
-
-long make_rand(void)
-{
-    return (labs(rand() + (rand() << 16)));
-}
-
-void init_rand(void)
-{
-    srand(getpid());
-}
-
-#endif

Modified: grass/branches/releasebranch_7_0/raster/r.spread/main.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.spread/main.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.spread/main.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -322,7 +322,8 @@
 	exit(EXIT_FAILURE);
 
 
-    srand(getpid());
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 
     display = flag.display->answer;
 #if 1

Modified: grass/branches/releasebranch_7_0/raster/r.spread/pick_dist.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.spread/pick_dist.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.spread/pick_dist.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -18,13 +18,14 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <math.h>
+#include <grass/gis.h>
 #include "local_proto.h"
 
 int pick_dist(int u)
 {
     int v;
 
-    v = (int)((u + 0.99999999999) * rand() / INT_MAX);
-    u = (int)((v + 0.99999999999) * rand() / INT_MAX);
-    return ((int)((u + 0.99999999999) * rand() / INT_MAX));	/*4th for a test */
+    v = (int)((u + 0.99999999999) * G_drand48());
+    u = (int)((v + 0.99999999999) * G_drand48());
+    return ((int)((u + 0.99999999999) * G_drand48()));	/*4th for a test */
 }

Modified: grass/branches/releasebranch_7_0/raster/r.spread/pick_ignite.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.spread/pick_ignite.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.spread/pick_ignite.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -13,6 +13,7 @@
  ******************************************************************************/
 #include <stdlib.h>
 #include <limits.h>
+#include <grass/gis.h>
 #include "local_proto.h"
 
 int p[18] = { 100, 100, 100, 90, 80, 70, 60, 50, 40,	/*adapt the "average" */
@@ -21,5 +22,5 @@
 
 int pick_ignite(int i)
 {
-    return ((100.0 * rand() / INT_MAX) <= p[i]);
+    return ((100.0 * G_drand48()) <= p[i]);
 }

Modified: grass/branches/releasebranch_7_0/raster/r.surf.fractal/spec_syn.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.surf.fractal/spec_syn.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.surf.fractal/spec_syn.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -38,14 +38,13 @@
 
     int row, col,		/* Counts through half the data array.  */
       row0, col0,		/* 'other half' of the array.           */
-      coeff,			/* No. of Fourier coeffficents to calc. */
-      seed;			/* Random number seed.                  */
+      coeff;			/* No. of Fourier coeffficents to calc. */
 
     double phase, rad,		/* polar coordinates of Fourier coeff.  */
      *temp[2];
 
-    seed = -1 * getpid();
-    G_math_rand(seed);		/* Reset random number generator.       */
+    /* FIXME - allow seed to be specified for repeatability */
+    G_math_srand_auto();	/* Reset random number generator.       */
 
     temp[0] = (double *)G_malloc(nn * nn * sizeof(double));
     temp[1] = (double *)G_malloc(nn * nn * sizeof(double));
@@ -65,12 +64,12 @@
 	for (col = 0; col <= nn / 2; col++) {
 	    /* Generate random Fourier coefficients. */
 
-	    phase = TWOPI * G_math_rand(0);
+	    phase = TWOPI * G_math_rand();
 
 	    if ((row != 0) || (col != 0))
 		rad =
 		    pow(row * row + col * col,
-			-(H + 1) / 2.0) * G_math_rand_gauss(0, 1.);
+			-(H + 1) / 2.0) * G_math_rand_gauss(1.);
 	    else
 		rad = 0.0;
 
@@ -101,10 +100,10 @@
 
     for (row = 1; row < nn / 2; row++)
 	for (col = 1; col < nn / 2; col++) {
-	    phase = TWOPI * G_math_rand(0);
+	    phase = TWOPI * G_math_rand();
 	    rad =
 		pow(row * row + col * col,
-		    -(H + 1) / 2.0) * G_math_rand_gauss(0, 1.);
+		    -(H + 1) / 2.0) * G_math_rand_gauss(1.);
 
 	    *(data[0] + row * nn + nn - col) = rad * cos(phase);
 	    *(data[1] + row * nn + nn - col) = rad * sin(phase);

Modified: grass/branches/releasebranch_7_0/raster/r.surf.gauss/gaussurf.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.surf.gauss/gaussurf.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.surf.gauss/gaussurf.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -32,7 +32,8 @@
 
 	/****** INITIALISE RANDOM NUMBER GENERATOR ******/
 
-    G_math_rand(-1 * getpid());
+    /* FIXME - allow seed to be specified for repeatability */
+    G_math_srand_auto();
 
 	/****** OPEN CELL FILES AND GET CELL DETAILS ******/
 
@@ -50,7 +51,7 @@
 	G_percent(row_count, nrows, 5);
 	for (col_count = 0; col_count < ncols; col_count++)
 	    *(row_out + col_count) =
-		(DCELL) (G_math_rand_gauss(2742, sigma) + mean);
+		(DCELL) (G_math_rand_gauss(sigma) + mean);
 
 	/* Write contents row by row */
 	Rast_put_d_row(fd_out, (DCELL *) row_out);

Modified: grass/branches/releasebranch_7_0/raster/r.surf.random/randsurf.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.surf.random/randsurf.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/raster/r.surf.random/randsurf.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -1,4 +1,3 @@
-#include <unistd.h>
 #include <math.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
@@ -21,7 +20,8 @@
     int row_count, col_count;
 
 	/****** INITIALISE RANDOM NUMBER GENERATOR ******/
-    G_math_rand(-1 * getpid());
+    /* FIXME - allow seed to be specified for repeatability */
+    G_math_srand_auto();
 
 	/****** OPEN CELL FILES AND GET CELL DETAILS ******/
     fd_out = Rast_open_new(out, int_map ? CELL_TYPE : DCELL_TYPE);
@@ -40,12 +40,12 @@
 	for (col_count = 0; col_count < ncols; col_count++) {
 	    if (int_map)
 		*(row_out_C + col_count) =
-		    (CELL) (G_math_rand(2742) * (max + 1 - min) + min);
+		    (CELL) (G_math_rand() * (max + 1 - min) + min);
 	    /* under represents first and last bin */
 	    /*                  *(row_out_C + col_count) = (CELL) floor(rand1(2742)*(max-min)+min +0.5); */
 	    else
 		*(row_out_D + col_count) =
-		    (DCELL) (G_math_rand(2742) * (max - min) + min);
+		    (DCELL) (G_math_rand() * (max - min) + min);
 	}
 
 	/* Write contents row by row */

Modified: grass/branches/releasebranch_7_0/vector/v.extract/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.extract/main.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/vector/v.extract/main.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -315,19 +315,15 @@
 	    G_fatal_error(_("Random category count is larger or equal to "
 			    "uniq <%s> feature category count %d"),
 			  opt.type->answer, ucat_count);
-
-	if (ucat_count >= RAND_MAX)
-	    G_fatal_error(_("There are more categories than random number "
-			    "generator can reach. Report this as a GRASS bug."));
 	
-	seed = getpid();
 	/* Initialise random number generator */
-	G_math_rand(-1 * seed);
+	/* FIXME - allow seed to be specified for repeatability */
+	G_math_srand_auto();
 
 	/* Fill cat_array with list of valid random numbers */
 	while (cat_count < nrandom) {
 	    /* Random number in range from 0 to largest CAT value */
-	    prnd = (int) floor(G_math_rand(seed) *
+	    prnd = (int) floor(G_math_rand() *
 			       (ucat_array[ucat_count - 1] + 1));
 	    qsort(cat_array, cat_count, sizeof(int), cmp);
 	    /* Check if generated number isn't already in 

Modified: grass/branches/releasebranch_7_0/vector/v.kcv/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.kcv/main.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/vector/v.kcv/main.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -19,25 +19,19 @@
 
 
 #include <stdlib.h>
-#include <unistd.h>
 #include <math.h>
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
-static double myrand(void)
-{
-    return rand() / (1.0 + RAND_MAX);
-}
-
 struct Cell_head window;
 
 int main(int argc, char *argv[]) 
 {
     int i, line, nlines, nlinks;
     int idx, *line_idx, lines_left;
-    double (*rng) ();
+    double (*rng)(void) = G_drand48;
     int nsites, p, np, min_count, spill;
     struct partition {
 	int id, count, max;
@@ -48,7 +42,6 @@
     struct line_cats *Cats;
     struct GModule *module;
     struct Option *map_opt, *col_opt, *npart_opt, *field_opt;
-    struct Flag *drand48_flag;
  
     /* Attributes */
     struct field_info *Fi;
@@ -81,15 +74,6 @@
     col_opt->description =
 	_("Name for new column to which partition number is written");
 
-    drand48_flag = G_define_flag();
-    drand48_flag->key = 'd';
-#ifdef HAVE_DRAND48
-    drand48_flag->description = _("Use drand48()");
-#else
-    drand48_flag->description = _("Use drand48() (ignored)");
-#endif
-
-
     G_gisinit(argv[0]);
 
     if (G_parser(argc, argv))
@@ -99,17 +83,8 @@
     if (np < 2)
 	G_fatal_error(_("'%s' must be > 1"), npart_opt->key);
 
-#ifdef HAVE_DRAND48
-    if (drand48_flag->answer) {
-	rng = drand48;
-	srand48((long)getpid());
-    }
-    else
-#endif
-    {
-	rng = myrand;
-	srand(getpid());
-    }
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 
     Points = Vect_new_line_struct();
     Cats = Vect_new_cats_struct();

Modified: grass/branches/releasebranch_7_0/vector/v.qcount/findquads.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.qcount/findquads.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/vector/v.qcount/findquads.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -6,28 +6,15 @@
  * Read the file GPL.TXT coming with GRASS for details.
  */
 
-#include <sys/types.h>		/* for getpid() */
-#include <unistd.h>
 #include <stdlib.h>
 #include <math.h>
 #include <grass/gis.h>
 #include <grass/vector.h>
 #include "quaddefs.h"
 
-#ifndef RAND_MAX
-#define RAND_MAX (pow(2.0,31.0)-1)
-#endif
+#define RANDOM(lo,hi) (G_drand48() * (hi-lo) + lo)
 
-#define R_INIT srand
-#define RANDOM(lo,hi) ((double) rand() / (double)RAND_MAX * (hi-lo) + lo)
-
 /*
-   #define R_MAX 1.0
-   #define R_INIT srand48
-   #define RANDOM(lo,hi) (drand48()/R_MAX*(hi-lo)+lo)
- */
-
-/*
  * returns Z struct filled with centers of n non-overlapping circles of
  * radius r contained completely within window
  */
@@ -42,8 +29,8 @@
     if (quads == NULL)
 	G_fatal_error("cannot allocate memory for quadrats");
 
-    srand((unsigned int)getpid());
-    /* R_INIT (1); */
+    /* FIXME - allow seed to be specified for repeatability */
+    G_srand48_auto();
 
     e_max = window.east - r;
     e_min = window.west + r;

Modified: grass/branches/releasebranch_7_0/vector/v.random/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.random/main.c	2014-07-31 11:24:06 UTC (rev 61470)
+++ grass/branches/releasebranch_7_0/vector/v.random/main.c	2014-07-31 11:25:00 UTC (rev 61471)
@@ -32,8 +32,6 @@
 
 #include <stdlib.h>
 #include <math.h>
-#include <sys/types.h>
-#include <unistd.h>
 #include <string.h>
 
 #include <grass/gis.h>
@@ -41,20 +39,6 @@
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#ifndef RAND_MAX
-#define RAND_MAX (pow(2.0,31.0)-1)
-#endif
-double myrand(void);
-
-#if defined(__CYGWIN__) || defined(__APPLE__) || defined(__MINGW32__)
-double drand48()
-{
-    return (rand() / (1.0 * RAND_MAX));
-}
-
-#define srand48(sv) (srand((unsigned)(sv)))
-#endif
-
 /* for qsort */
 
 typedef struct {
@@ -77,10 +61,10 @@
 int main(int argc, char *argv[])
 {
     char *output, buf[DB_SQL_MAX];
-    double (*rng) ();
-    double max, zmin, zmax;
+    double (*rng)(void) = G_drand48;
+    double zmin, zmax;
     int seed;
-    int i, j, k, n, b, type, usefloat;
+    int i, j, k, n, type, usefloat;
     int area, nareas, field;
     struct boxlist *List = NULL;
     BOX_SIZE *size_list = NULL;
@@ -99,7 +83,7 @@
     } parm;
     struct
     {
-	struct Flag *rand, *drand48, *z, *notopo, *a;
+	struct Flag *z, *notopo, *a;
     } flag;
     struct field_info *Fi;
     dbDriver *driver;
@@ -187,10 +171,6 @@
     flag.a->key = 'a';
     flag.a->description = _("Generate n points for each individual area");
 
-    flag.drand48 = G_define_flag();
-    flag.drand48->key = 'd';
-    flag.drand48->description = _("Use drand48() function instead of rand()");
-
     flag.notopo = G_define_standard_flag(G_FLG_V_TOPO);
 
     if (G_parser(argc, argv))
@@ -198,7 +178,6 @@
 
     output = parm.output->answer;
     n = atoi(parm.nsites->answer);
-    b = flag.drand48->answer ? TRUE : FALSE;
     
     if(parm.seed->answer)
         seed = atoi(parm.seed->answer);
@@ -304,24 +283,11 @@
 
     Vect_hist_command(&Out);
 
-    if (b) {
-	rng = drand48;
-	max = 1.0;
-	/* Init the random seed */
-	if(parm.seed->answer)
-	    srand48((long)seed);
-	else
-	    srand48((long)getpid());
-    }
-    else {  	/* default is rand() */
-	rng = myrand;
-	max = RAND_MAX;
-	/* Init the random seed */
-	if(parm.seed->answer)
-	    srand(seed);
-	else
-	    srand(getpid());
-    }
+    /* Init the random seed */
+    if(parm.seed->answer)
+	G_srand48(seed);
+    else
+	G_srand48_auto();
 
     G_get_window(&window);
 
@@ -462,9 +428,9 @@
 		Vect_reset_cats(Cats);
 
 		while (outside) {
-		    x = rng() / max * (bbox.W - bbox.E) + bbox.E;
-		    y = rng() / max * (bbox.N - bbox.S) + bbox.S;
-		    z = rng() / max * (zmax - zmin) + zmin;
+		    x = rng() * (bbox.W - bbox.E) + bbox.E;
+		    y = rng() * (bbox.N - bbox.S) + bbox.S;
+		    z = rng() * (zmax - zmin) + zmin;
 
 		    ret = Vect_point_in_area(x, y, &In, area, &abox);
 
@@ -512,9 +478,9 @@
 	    Vect_reset_line(Points);
 	    Vect_reset_cats(Cats);
 
-	    x = rng() / max * (window.west - window.east) + window.east;
-	    y = rng() / max * (window.north - window.south) + window.south;
-	    z = rng() / max * (zmax - zmin) + zmin;
+	    x = rng() * (window.west - window.east) + window.east;
+	    y = rng() * (window.north - window.south) + window.south;
+	    z = rng() * (zmax - zmin) + zmin;
 	    
 	    if (nareas) {
 		int outside = 1;
@@ -588,9 +554,9 @@
 			}
 		    }
 		    if (outside) {
-			x = rng() / max * (window.west - window.east) + window.east;
-			y = rng() / max * (window.north - window.south) + window.south;
-			z = rng() / max * (zmax - zmin) + zmin;
+			x = rng() * (window.west - window.east) + window.east;
+			y = rng() * (window.north - window.south) + window.south;
+			z = rng() * (zmax - zmin) + zmin;
 		    }
 		} while (outside);
 	    }
@@ -635,8 +601,3 @@
 
     exit(EXIT_SUCCESS);
 }
-
-double myrand()
-{
-    return (double)rand();
-}



More information about the grass-commit mailing list