[GRASS5] r.random broken

Hamish hamish_nospam at yahoo.com
Tue Jul 5 01:59:57 EDT 2005


> A month ago, I noted that r.random produces points all clustered along
> the top of a region, instead of randomly distributed (see attached
> output from r.random for 1000 points). You quickly identified the
> problem. But it looks like it hasn¹t been fixed yet.

Following fix applied to 6.1-cvs.

In order to keep things generic, I applied this version:
  (I have no idea what compilers people use)

#define lrand48() ((long)((double) rand() * (1<<31) / RAND_MAX))


> Could this be fixed for 6.0.1?

Could someone please test it on Mac & Cygwin, if it works on both we can
apply it to the 6.0 release branch too.



Hamish

======================================================================
http://grass.itc.it/pipermail/grass5/2005-June/018528.html
[here slighly cropped -HB]


From: Glynn Clements
Date: Sun, 5 Jun 2005 01:12:05 +0100

> > > > > lrand48() is supposed to return random numbers between 0 and
> > > > > 2^31. The above lrand48() macro will end up returning values
> > > > > which are far too small, so, the random test will return true
> > > > > too often, resulting in the desired number of random cells
> > > > > being reached far too early.
..
> lrand48() returns a value between 0 and 2^31.
> 
> rand() returns a value between 0 and RAND_MAX, where RAND_MAX is an
> implementation-defined constant.
> 
> To emulate lrand48() using rand(), you would need something like:
> 
> 	#define lrand48() (rand() * (1<<31) / RAND_MAX)
> 
> Except that the above will overflow. To prevent that, convert to
> double then back to long, e.g.:
> 
> 	#define lrand48() ((long)((double) rand() * (1<<31) / RAND_MAX))
> 
> Or, if your compiler supports it, use "long long":
> 
> 	#define lrand48() ((long)(rand() * (1LL<<31) / RAND_MAX))




More information about the grass-dev mailing list