cast(abs(random()) / 9223372036854775807.0 * 7 as integer) Will fix that. You throw away one bit of randomness (by the abs()), convert to a floating point value in the range of 0 but less than 1 (throwing away a few more bits in the conversion (since the precision of a double mantissa is only 53 bits, not 63)), divide that into 7 buckets, and take the floor of that as an integer result. This will have a uniformity equal to the underlying PRNG.
The bias inherent in a direct mod operation is insignificant when you are using 64 bits of randomness each time ... the non-uniformity of the PRNG outweighs the bias of the modulus calculation at least for small values of the modulus. --- The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume. >-----Original Message----- >From: sqlite-users [mailto:sqlite-users- >boun...@mailinglists.sqlite.org] On Behalf Of Jens Alfke >Sent: Monday, 9 July, 2018 10:49 >To: SQLite mailing list >Subject: Re: [sqlite] Kind of pivot table > > >> 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+bi >as&ie=UTF-8&oe=UTF-8 ><https://www.google.com/search?client=safari&rls=en&q=random+modulo+b >ias&ie=UTF-8&oe=UTF-8> > >—Jens >_______________________________________________ >sqlite-users mailing list >sqlite-users@mailinglists.sqlite.org >http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users