[GRASS5] r.random broken

Markus Neteler neteler at itc.it
Wed Jul 6 07:27:00 EDT 2005


Hi,

there is probably another canditate:

~/grass61/raster/r.mapcalc > grep lrand *
xrand.c:#define lrand48() ((long)rand())
xrand.c:                        long x = lrand48();

Markus

> On 7/4/05 10:59 PM, "Hamish" <hamish_nospam at yahoo.com> wrote:
> 
> >> 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))
> 
> ____________________
> C. Michael Barton, Professor of Anthropology
> School of Human Evolution and Social Change
> PO Box 872402
> Arizona State University
> Tempe, AZ  85287-2402
> USA
> 
> Phone: 480-965-6262
> Fax: 480-965-7671
> www: <www.public.asu.edu/~cmbarton>
> 




More information about the grass-dev mailing list