[Coco] Re: 6809 example
William Astle
lost at l-w.net
Sun Jan 18 05:37:11 EST 2004
On Sat, 17 Jan 2004, Roger Taylor wrote:
> Yep, RND(0) was the only way on some ancient personal computers, so it gets
> used a lot even now. RND(0) always returns a value >0 and <1 and you have
> to multiply it by a factor to scale it up. Simply pointless unless you
> know something about the random generator that proves to give better
> results this way.
Out of curiosity, I looked up the code for RND in the unravelled series.
It uses the following formula to calculate a random number between 1 and n
inclusive:
INT(RND(0)*INT(n)+1)
So, it turns out that if you need a zero-based random number (0 <= x < n),
it is likely faster to do this yourself:
INT(RND(0)*n)
Note that the INT() is gone from the "n" part. Since you can guarantee you
have an integer scale there, you don't need it. And if you don't need to
guarantee the result is an integer, for example when the next operation
will force it integer anyway, then
RND(0)*n
will have a reasonable speed increase.
On top of everything, by doing the scaling yourself, you can do some cute
games like getting a floating point random number such that 1.75 < x <
77.3 if you want. :)
--
William Astle
finger lost at l-w.net for further information
Geek Code V3.12: GCS/M/S d- s+:+ !a C++ UL++++$ P++ L+++ !E W++ !N w--- !O
!M PS PE V-- Y+ PGP t+@ 5++ X !R tv+@ b+++@ !DI D? G e++ h+ y?
More information about the Coco
mailing list