[GRASS-dev] [bug #4998] (grass) r.mapcalc rand (a, b): b-1 is used instead of b

Glynn Clements glynn at gclements.plus.com
Mon Aug 14 06:30:04 EDT 2006


Request Tracker wrote:

> this bug's URL: http://intevation.de/rt/webrt?serial_num=4998
> -------------------------------------------------------------------------
> 
> Subject: r.mapcalc rand (a,b): b-1 is used instead of b
> 
> Platform: GNU/Linux/x86
> grass obtained from: CVS
> grass binary for platform: Compiled from Sources
> GRASS Version: 2006-08-12
> 
> The rand (a,b) function in r.mapcalc uses the (b-1) instead of b:
> 
> $ r.mapcalc 'map=rand(1,100)'
> $ r.info -r map
> min=1
> max=99
>     ^
>     |
>   should be 100!

The above is correct, although the documentation should be more
explicit. The result of rand() is a pseudo-random value x such that:

	a <= x < b

This definition results in consistent behaviour for both the integer
and floating-point cases, as well as being consistent with most
standard PRNG functions (which usually treat the upper bound as
exclusive).

> And if the min and max are equal, r.mapcals goes into some infinite loop:
> 
> $ r.mapcalc 'map=rand(1,1)'
>    0%
>    ^
>    |
>   no progress whatever, 100% CPU used

This might a compiler error, although it should handle this
explicitly.

The above case will result in division by zero, typically causing
SIGFPE to be thrown. r.mapcalc installs a signal handler for SIGFPE
which sets a flag then returns. It appears that SIGFPE gets thrown
continuously in this case.

I've fixed this error, and updated the documentation to clarify the
behaviour.

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




More information about the grass-dev mailing list