[GRASS-dev] using rand(x,y) in r.mapcalc (grass7)

Glynn Clements glynn at gclements.plus.com
Wed Jul 2 17:15:52 PDT 2014


Paulo van Breugel wrote:

> When I run several times e.g., r.mapcalc "a = rand(0,100)"
> 
> I am always getting exactly the same layer. In the help file it reads:
> 
> "The environment variable GRASS_RND_SEED is read to initialize the random
> number generator"
> 
> But what does it mean.

The value of that environment variable is parsed using atol() and the
result used to seed the PRNG (via srand() or srand48()) (setup_rand()
in r.mapcalc/evaluate.c).

If the variable isn't set, the PRNG isn't explicitly seeded. For
rand(), the result should be equivalent to GRASS_RND_SEED=1.

> Shouldn't the seed not be generated on e.g, OS time,
> which would ensure that each run would give a different result?

No. The reason is to provide reproducibility. Anyone running the same
command with the same data should obtain the same result.

If you want a different result each time, set GRASS_RND_SEED to a
different value each time, e.g.

	GRASS_RND_SEED=`date +%N` r.mapcalc "a = rand(0,100)"

[%N is the nanoseconds portion of the current time; this is a GNU
extension.]

> On a related note, it would be nice to be able to set the seed (I think
> there has been such a request before, but not sure about the answer at that
> time).

GRASS_RND_SEED was the answer.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list