[GRASS-SVN] r69620 - in grass/trunk/raster/r.sim: r.sim.sediment r.sim.water simlib
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Oct 1 13:48:55 PDT 2016
Author: annakrat
Date: 2016-10-01 13:48:55 -0700 (Sat, 01 Oct 2016)
New Revision: 69620
Modified:
grass/trunk/raster/r.sim/r.sim.sediment/main.c
grass/trunk/raster/r.sim/r.sim.water/main.c
grass/trunk/raster/r.sim/simlib/simlib.h
Log:
simlib: give more control over seeding of random number generator
Modified: grass/trunk/raster/r.sim/r.sim.sediment/main.c
===================================================================
--- grass/trunk/raster/r.sim/r.sim.sediment/main.c 2016-10-01 19:51:11 UTC (rev 69619)
+++ grass/trunk/raster/r.sim/r.sim.sediment/main.c 2016-10-01 20:48:55 UTC (rev 69620)
@@ -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/trunk/raster/r.sim/r.sim.water/main.c
===================================================================
--- grass/trunk/raster/r.sim/r.sim.water/main.c 2016-10-01 19:51:11 UTC (rev 69619)
+++ grass/trunk/raster/r.sim/r.sim.water/main.c 2016-10-01 20:48:55 UTC (rev 69620)
@@ -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/trunk/raster/r.sim/simlib/simlib.h
===================================================================
--- grass/trunk/raster/r.sim/simlib/simlib.h 2016-10-01 19:51:11 UTC (rev 69619)
+++ grass/trunk/raster/r.sim/simlib/simlib.h 2016-10-01 20:48:55 UTC (rev 69620)
@@ -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