[GRASS5] r.random broken
Michael Barton
michael.barton at asu.edu
Tue Jul 5 10:17:53 EDT 2005
Hamish,
Thanks very much. I don't have the setup to compile to test. Though if a new
binary comes out in the next few days I will give it a go.
Michael
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