[GRASS5] r.random broken
Glynn Clements
glynn at gclements.plus.com
Sat Jun 4 20:12:05 EDT 2005
Hamish wrote:
> > > > 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.
> > ..
> >
> > just replace 32767.0 with 2^31. Maybe try-
>
>
> sorry, ignore that.
>
> better:
>
> #define lrand48() rand()*65536.
Still wrong.
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))
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-dev
mailing list