Re: [RFC PATCH] random: add get_random_max() function

2019-03-24 Thread George Spelvin
P.S. The cited paper calls your algorithm the "OpenBSD algorithm" and has a bunch of benchmarks comparing it to others in Fisher-Yates shuffles of sizes 1e3..1e9. Including all overhead (base PRNG, shuffle), it's 3x slower for 32-bit operations and 8x slower for 64-bit up to arrays of size 1e6, af

Re: [RFC PATCH] random: add get_random_max() function

2019-03-24 Thread George Spelvin
On Sun, 24 Mar 2019 at 21:47:50 +0100, Jason A. Donenfeld wrote: > I generally use a slightly simpler algorithm in various different projects: > > //[0, bound) > static unsigned long random_bounded(unsigned long bound) > { >unsigned long ret; >const unsigned long max_mod_bound = (1

Re: [RFC PATCH] random: add get_random_max() function

2019-03-24 Thread Jason A. Donenfeld
I generally use a slightly simpler algorithm in various different projects: //[0, bound) static unsigned long random_bounded(unsigned long bound) { unsigned long ret; const unsigned long max_mod_bound = (1 + ~bound) % bound; if (bound < 2) return 0; do