~ chicken-core (chicken-5) 3ab72934553755cd54e1a870245137367c500cd8
commit 3ab72934553755cd54e1a870245137367c500cd8 Author: felix <felix@call-with-current-continuation.org> AuthorDate: Sun Nov 5 00:03:47 2017 +0100 Commit: felix <felix@call-with-current-continuation.org> CommitDate: Sun Nov 5 00:03:47 2017 +0100 better random_uniform, uses libsodium/arc4random approach, suggested by Kooda diff --git a/runtime.c b/runtime.c index 62e76aeb..d12d90e0 100644 --- a/runtime.c +++ b/runtime.c @@ -12620,11 +12620,18 @@ static C_uword random_word(void) static C_uword random_uniform(C_uword bound) { - C_uword r; + C_uword r, min; - do { r = random_word(); } while(r >= bound); + if (bound < 2) return 0; - return r; + min = (1U + ~bound) % bound; /* = 2**<wordsize> mod bound */ + + do r = random_word(); while (r < min); + + /* r is now clamped to a set whose size mod upper_bound == 0 + * the worst case (2**<wordsize-1>+1) requires ~ 2 attempts */ + + return r % bound; }Trap