Browse Source

r.random: add seed option to set the seed of the RNG (fixes https://trac.osgeo.org/grass/ticket/3594)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@72886 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 7 years ago
parent
commit
f36a0d89f0
4 changed files with 19 additions and 23 deletions
  1. 0 14
      raster/r.random/creat_rand.c
  2. 0 6
      raster/r.random/local_proto.h
  3. 18 1
      raster/r.random/main.c
  4. 1 2
      raster/r.random/random.c

+ 0 - 14
raster/r.random/creat_rand.c

@@ -1,14 +0,0 @@
-#include <grass/gis.h>
-
-
-long make_rand(void)
-{
-    return G_lrand48();
-}
-
-void init_rand(void)
-{
-    /* FIXME - allow seed to be specified for repeatability */
-    G_srand48_auto();
-}
-

+ 0 - 6
raster/r.random/local_proto.h

@@ -44,12 +44,6 @@ struct rr_state
 /* count.c */
 void get_stats(struct rr_state *);
 
-/* creat_rand.c */
-long make_rand(void);
-void init_rand(void);
-long make_rand(void);
-void init_rand(void);
-
 /* random.c */
 int execute_random(struct rr_state *);
 

+ 18 - 1
raster/r.random/main.c

@@ -34,11 +34,12 @@ int main(int argc, char *argv[])
     gcell_count targets;
     gcell_count count;
     struct rr_state myState;
+    long seed_value;
 
     struct GModule *module;
     struct
     {
-	struct Option *input, *cover, *raster, *sites, *npoints;
+	struct Option *input, *cover, *raster, *sites, *npoints, *seed;
     } parm;
     struct
     {
@@ -81,6 +82,12 @@ int main(int argc, char *argv[])
     parm.sites->required = NO;
     parm.sites->key = "vector";
 
+    parm.seed = G_define_option();
+    parm.seed->key = "seed";
+    parm.seed->type = TYPE_INTEGER;
+    parm.seed->required = NO;
+    parm.seed->description = _("Seed for rand() function");
+
     flag.zero = G_define_flag();
     flag.zero->key = 'z';
     flag.zero->description = _("Generate points also for NULL category");
@@ -194,6 +201,16 @@ int main(int argc, char *argv[])
 	myState.nRand = targets;
     }
 
+    if (parm.seed->answer) {
+        seed_value = atol(parm.seed->answer);
+        G_srand48(seed_value);
+        G_debug(3, "Read random seed from seed=: %ld", seed_value);
+    }
+    else {
+        seed_value = G_srand48_auto();
+        G_debug(3, "Generated random seed (-s): %ld", seed_value);
+    }
+
     execute_random(&myState);
 
     if (myState.outraster)

+ 1 - 2
raster/r.random/random.c

@@ -116,7 +116,6 @@ int execute_random(struct rr_state *theState)
 
     G_percent(0, theState->nRand, 2);
 
-    init_rand();
     nc = (theState->use_nulls) ? theState->nCells :
 	theState->nCells - theState->nNulls;
     nt = theState->nRand;	/* Number of points to generate */
@@ -141,7 +140,7 @@ int execute_random(struct rr_state *theState)
 		    do_check = 0;
 	    }
 
-	    if (do_check && make_rand() % nc < nt) {
+	    if (do_check && G_lrand48() % nc < nt) {
 		nt--;
 		if (is_null_value(theState->buf, col))
 		    cpvalue(&theState->nulls, 0, &theState->buf, col);