Skip to content

Commit

Permalink
lib: rand - Fix potential modulo bias
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse committed Aug 7, 2018
1 parent 8c6c5ab commit 75c248e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/rand.c
Expand Up @@ -27,7 +27,8 @@ uint32_t i_rand(void)

uint32_t i_rand_limit(uint32_t upper_bound)
{
/* FIXME: This simple implementation suffers from modulo-bias. */
return i_rand() % upper_bound;
uint32_t val, min = -upper_bound % upper_bound;
while((val = i_rand()) < min);
return val % upper_bound;
}
#endif

0 comments on commit 75c248e

Please sign in to comment.