Skip to content

Commit

Permalink
v.perturb: Require either explicit seed or generated seed (#1661)
Browse files Browse the repository at this point in the history
This moves the modules to the standard followed by r.mapcalc where we require
user to either provide seed or to ask for a seed with -s flag
(-s results in a different seed and thus different results every time).

The original code used fixed seed by default and required explicit
change to get a different 'random' result.
The GRASS standard G_srand48_auto() function is used which returns the seed
which is in turn used in the custom random number generator code in the module.
  • Loading branch information
wenzeslaus committed Oct 7, 2021
1 parent 5e86162 commit c26c04f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions vector/v.perturb/main.c
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv)
struct
{
struct Option *in, *out, *dist, *pars, *min, *seed, *field;
struct Flag *no_topo;
struct Flag *gen_seed, *no_topo;
} parm;

G_gisinit(argv[0]);
Expand Down Expand Up @@ -105,16 +105,30 @@ int main(int argc, char **argv)
parm.seed->key = "seed";
parm.seed->type = TYPE_INTEGER;
parm.seed->required = NO;
parm.seed->answer = "0";
parm.seed->description = _("Seed for random number generation");


parm.gen_seed = G_define_flag();
parm.gen_seed->key = 's';
parm.gen_seed->description = _("Generate random seed (result is non-deterministic)");

parm.no_topo = G_define_standard_flag(G_FLG_V_TOPO);

/* Either explicit seed or explicitly generate seed, but not both. */
G_option_exclusive(parm.seed, parm.gen_seed, NULL);
G_option_required(parm.seed, parm.gen_seed, NULL);

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

min = atof(parm.min->answer);
seed = atoi(parm.seed->answer);
if (parm.seed->answer) {
seed = atol(parm.seed->answer);
G_debug(3, "Read random seed from seed=: %d", seed);
}
else {
seed = G_srand48_auto();
G_debug(3, "Generated random seed (-s): %d", seed);
}

switch (parm.dist->answer[0]) {
case 'u':
Expand Down

0 comments on commit c26c04f

Please sign in to comment.