summaryrefslogtreecommitdiff
path: root/src/librandom/random_uint32_from.c
blob: 621c438852f4ee8e45073d5791da7a11cf31d318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* ISC license. */

#include <skalibs/uint32.h>
#include <skalibs/functypes.h>
#include <skalibs/random.h>

uint32_t random_uint32_from (uint32_t n, randomgen_func_ref f)
{
  uint32_t min, x ;
  if (n < 2) return 0 ;
  min = -n % n ;
  for (;;)
  {
    char tmp[4] ;
    (*f)(tmp, 4) ;
    uint32_unpack_big(tmp, &x) ;
    if (x >= min) break ;
  }
  return x % n ;
}