Re: Swapping is inefficient and wasteful

From: Brian Hetrick (bhetrick_at_notinnedmeats.iname.com)
Date: 11/24/04


Date: Wed, 24 Nov 2004 08:11:57 -0500


"Ernst Lippe" <ernstl-at-planet-dot-nl@ignore.this> wrote ...
> That depends on the bitsize of your floats, in this way you
> can get only 2**24 different output values, and when your
> floats have a mantissa of more than 24 bits there are some
> output values that your algorithm can't produce.

True. However, an IEEE single has 24 significant bits, which seems a
reasonable number to use in an example. If there is a need for more
significant bits, the obvious extension can be used.

More interesting, because more common, is the need for an unbiased
choice among a set of alternatives on machines (such as microcontrol-
lers) where there is no floating point unit. Rather than construct a
floating point package, you can join two random bytes to form a sample
from U{0,1,...,65535}, multiply by the (say, one-byte) number of
alternatives, and take the top byte of the three-byte product.