by colmmacc · 1mo ago · 0 comments
For sampling from a distribution, the best method is Vose's Alias. https://www.keithschwarz.com/darts-dice-coins/ is my favorite write up.
The article linked by you is slightly misleading, because this paragraph is not really right:
> If you are willing to suffer a slight statistical bias, you can generate a floating-point values in [0,1) and multiply the result by the size of the interval. It avoids the division but introduces other overhead.
If you want to use multiplication instead of division there is no need whatsoever to generate random floating-point numbers.
You just interpret the random integer as a fixed-point fraction and you must use the unsigned integer multiply instruction that provides a double length product (or in some ISAs you must use the instruction that provides the high half of the product), from which you can extract the bits corresponding to the integer part of the result.
Thus there is no overhead in comparison with the division method. You should just use for input a PRNG for which all 2^N values are equiprobable, which is the case for most of them (there are some ancient PRNGs that provide only the non-negative signed integers; such PRNGs are wasting a bit and they must be shifted left by one before multiplication).
Moreover, there is absolutely no need to be "willing to suffer a slight statistical bias". If you apply rejection either before or after multiplication, you can remove any statistical bias, exactly like when you use division with rejection. You just need to be careful how you implement rejection, because the rejection criteria are different than for the division method. E.g., if you do the rejection after multiplication, you reject a result if its fractional part is greater than a constant that has been computed as the fractional part of the product between the multiplicand (i.e. the number of output values) and the greatest possible input value (2^N-1 interpreted as a fraction). This rejection ensures that the same number of input values fall in each output interval of unit length. When the number of output values is small in comparison with the number of input values, rejection happens very seldom.
While not saying this clearly, the "nearlydivisionless" function actually does exactly what I have said above, after reading just that introductory paragraph, which shows a straw man for comparison, but it is both more complex and slower than the multiplication method implemented correctly, which never requires any division. The "nearlydivisionless" function also removes the statistical bias by rejecting the result based on its fractional part, but instead of comparing with a precomputed constant it uses an inefficient method of computing the rejection threshold during function invocations, and using a division, even if the threshold can also be computed with a multiplication.
Moreover, I believe that the right implementation should use inline assembly for the multiply instruction, which provides guaranteed performance. The "nearlydivisionless" function avoids the inline assembly by writing some excessively complicated expression, with the hope that a clever compiler will optimize it by simplifying it to something equivalent with the correct assembly instruction. I do not believe that relying on very clever compilers is the right choice for achieving the portability of a program. On the contrary, even when recompiling the program for the same ISA, a new version of the same compiler may not do the desired optimization, causing unpredictable performance problems. If the compiler fails to optimize the 128-bit multiplication written in the "nearlydivisionless" function, by noticing that both 128-bit operands have been recently promoted from 64-bit values, it would do 4 multiplications instead of 1 multiplication.
Like I have said, I think that Daniel Lemire has another more recent article, including better multiplication methods, but I am too lazy to search for it now.