[GRASS-SVN] r70201 - in grass/branches/releasebranch_7_2: . raster/r.sim/r.sim.sediment raster/r.sim/r.sim.water raster/r.sim/simlib

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 2 08:13:07 PST 2017


Author: annakrat
Date: 2017-01-02 08:13:07 -0800 (Mon, 02 Jan 2017)
New Revision: 70201

Modified:
   grass/branches/releasebranch_7_2/
   grass/branches/releasebranch_7_2/raster/r.sim/r.sim.sediment/main.c
   grass/branches/releasebranch_7_2/raster/r.sim/r.sim.water/main.c
   grass/branches/releasebranch_7_2/raster/r.sim/simlib/simlib.h
Log:
simlib: give more control over seeding of random number generator (merge from trunk, r69620)


Property changes on: grass/branches/releasebranch_7_2
___________________________________________________________________
Added: svn:mergeinfo
   + /grass/trunk:69619-69620

Modified: grass/branches/releasebranch_7_2/raster/r.sim/r.sim.sediment/main.c
===================================================================
--- grass/branches/releasebranch_7_2/raster/r.sim/r.sim.sediment/main.c	2017-01-02 16:11:08 UTC (rev 70200)
+++ grass/branches/releasebranch_7_2/raster/r.sim/r.sim.sediment/main.c	2017-01-02 16:13:07 UTC (rev 70201)
@@ -101,10 +101,11 @@
 {
     int ii;
     int ret_val;
-    static int rand1 = 12345;
     struct Cell_head cellhd;
     struct WaterParams wp;
     struct options parm;
+    struct flags flag;
+    long seed_value;
 
     G_gisinit(argv[0]);
 
@@ -259,9 +260,42 @@
     parm.diffc->description = _("Water diffusion constant");
     parm.diffc->guisection = _("Parameters");
 
+    
+    parm.seed = G_define_option();
+    parm.seed->key = "random_seed";
+    parm.seed->type = TYPE_INTEGER;
+    parm.seed->required = NO;
+    parm.seed->label = _("Seed for random number generator");
+    parm.seed->description =
+        _("The same seed can be used to obtain same results"
+          " or random seed can be generated by other means.");
+
+    flag.generateSeed = G_define_flag();
+    flag.generateSeed->key = 's';
+    flag.generateSeed->label =
+        _("Generate random seed");
+    flag.generateSeed->description =
+        _("Automatically generates random seed for random number"
+          " generator (use when you don't want to provide the seed option)");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    if (flag.generateSeed->answer) {
+        seed_value = G_srand48_auto();
+        G_verbose_message(_("Generated random seed (-s): %ld"), seed_value);
+    }
+    else if (parm.seed->answer) {
+        seed_value = atol(parm.seed->answer);
+        G_srand48(seed_value);
+        G_verbose_message(_("Read random seed from %s option: %ld"),
+                          parm.seed->key, seed_value);
+    }
+    else {
+        /* default as it used to be */
+        G_srand48(12345);
+    }
+
     G_get_set_window(&cellhd);
 
     WaterParams_init(&wp);
@@ -365,7 +399,6 @@
 
     alloc_grids_sediment();
 
-    G_srand48(rand1);
     grad_check();
     init_grids_sediment();
     /* treba dat output pre topoerdep */

Modified: grass/branches/releasebranch_7_2/raster/r.sim/r.sim.water/main.c
===================================================================
--- grass/branches/releasebranch_7_2/raster/r.sim/r.sim.water/main.c	2017-01-02 16:11:08 UTC (rev 70200)
+++ grass/branches/releasebranch_7_2/raster/r.sim/r.sim.water/main.c	2017-01-02 16:13:07 UTC (rev 70201)
@@ -100,12 +100,12 @@
     int ii;
     int ret_val;
     double x_orig, y_orig;
-    static int rand1 = 12345;
     struct GModule *module;
     struct Cell_head cellhd;
     struct WaterParams wp;
     struct options parm;
     struct flags flag;
+    long seed_value;
 
     G_gisinit(argv[0]);
 
@@ -297,9 +297,41 @@
     flag.tserie->description = _("Time-series output");
     flag.tserie->guisection = _("Output");
 
+    parm.seed = G_define_option();
+    parm.seed->key = "random_seed";
+    parm.seed->type = TYPE_INTEGER;
+    parm.seed->required = NO;
+    parm.seed->label = _("Seed for random number generator");
+    parm.seed->description =
+        _("The same seed can be used to obtain same results"
+          " or random seed can be generated by other means.");
+
+    flag.generateSeed = G_define_flag();
+    flag.generateSeed->key = 's';
+    flag.generateSeed->label =
+        _("Generate random seed");
+    flag.generateSeed->description =
+        _("Automatically generates random seed for random number"
+          " generator (use when you don't want to provide the seed option)");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    if (flag.generateSeed->answer) {
+        seed_value = G_srand48_auto();
+        G_verbose_message(_("Generated random seed (-s): %ld"), seed_value);
+    }
+    else if (parm.seed->answer) {
+        seed_value = atol(parm.seed->answer);
+        G_srand48(seed_value);
+        G_verbose_message(_("Read random seed from %s option: %ld"),
+                          parm.seed->key, seed_value);
+    }
+    else {
+        /* default as it used to be */
+        G_srand48(12345);
+    }
+
     G_get_set_window(&cellhd);
 
     WaterParams_init(&wp);
@@ -476,8 +508,6 @@
 
     alloc_grids_water();
 
-    G_debug(1, "seeding randoms");
-    G_srand48(rand1);
     grad_check();
     main_loop();
 

Modified: grass/branches/releasebranch_7_2/raster/r.sim/simlib/simlib.h
===================================================================
--- grass/branches/releasebranch_7_2/raster/r.sim/simlib/simlib.h	2017-01-02 16:11:08 UTC (rev 70200)
+++ grass/branches/releasebranch_7_2/raster/r.sim/simlib/simlib.h	2017-01-02 16:13:07 UTC (rev 70201)
@@ -85,12 +85,12 @@
 	*observation, *depth, *disch, *err, *outwalk, *nwalk, *niter, *outiter,
 	*density, *diffc, *hmax, *halpha, *hbeta, *wdepth, *detin, *tranin,
 	*tauin, *tc, *et, *conc, *flux, *erdep, *rainval, *maninval,
-	*infilval, *logfile;
+	*infilval, *logfile, *seed;
 };
 
 struct flags
 {
-    struct Flag *mscale, *tserie;
+    struct Flag *mscale, *tserie, *generateSeed;
 };
 
 #endif /* __SIMLIB_H__ */



More information about the grass-commit mailing list