Re: [AD] Allegro 4.2 todos

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


> > The current formula is:
> >
> > ((rand() >> 8) + (rand() & 0xff00))
> >
> > As previously suggested on this list. I've found the following formula
> > works better under OSX though:
> >
> > ((rand() >> 16) + rand())
>
> As Ben (IIRC) pointed out, this one is not guaranteed to give a
non-negative
> result. We must clamp somewhere
>
> > I've tested other shift values between 8 and 16, but the best remains 16
>
> Then
>
> ((rand() >> 16) + (rand() & 0x3fffffff)  ?
>
> > So I wonder: is this due to the machine endianess or just OSX rand()
> > implementation? I think the latter, but could someone test the thing
under
> > another big endian machine and report the results?
>
> I think it is simply the particular implementation of the OS

My 2 cents,

If the point of all this is to make a *more random* random function, you
could just use rand()
without the % operator, that only uses the low bits and do something like
this that would use the whole
rand range.
And since you seem to want all this for allegro examples and/or as a private
function...

// returns a number in the range [min, max)
int myrand(int min, int max) {
  double x = (double)rand() / (double)(RAND_MAX + 1);
  x *= max - min;
  x += min;
  return (int)x;
}

That (I think) should work for any OS, endianness etc





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/