lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>               "If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in
> 
>                      j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

That's almost exactly what Lua does:
	http://www.lua.org/source/5.1/lmathlib.c.html#math_random

except for a concession to a SunOS bug and for dividing by RAND_MAX instead
of by RAND_MAX+1.

> I wonder if some variant of the advice could be followed to allow the return
> value to be more completely utilized, without having to resort to tricks like
> calling a rand() a few times to discard the start of the run?

At least the trick is trivial to implement and seems to cure the problem...
--lhf