On 7/9/18 12:48 PM, Jens Alfke wrote:
>> On Jul 7, 2018, at 11:49 PM, Keith Medcalf <kmedc...@dessus.com> wrote:
>> Why not use MOD (%) as in
>> ABS(RANDOM() % 6)
> Because modulo bias. If the RHS doesn't evenly divide the size of the range 
> of RANDOM(), some results will be slightly more likely than others.
> https://zuttobenkyou.wordpress.com/2012/10/18/generating-random-numbers-without-modulo-bias/
> <https://zuttobenkyou.wordpress.com/2012/10/18/generating-random-numbers-without-modulo-bias/>
> Of course in this case, where the range of RANDOM() is probably 2^32, the 
> bias will be very small. But in some use cases the bias can be magnified by 
> subsequent operations and can still skew the eventual result. A Google search 
> turns up several hits that promise fairer algorithms.
> https://www.google.com/search?client=safari&rls=en&q=random+modulo+bias&ie=UTF-8&oe=UTF-8
> <https://www.google.com/search?client=safari&rls=en&q=random+modulo+bias&ie=UTF-8&oe=UTF-8>
> —Jens
But ANY random number generator that generates a finite number of
results is almost certainly going to have a similar bias, as you divide
up those numbers into bins. To get rid of the bias you generally need to
throw out some values so you have an equal number in each bin (unless
you luck out and start having an even number in each bin). Your second
solution did this. If your first solution was good enough, so should
have MOD.

Richard Damon

sqlite-users mailing list

Reply via email to