summaryrefslogtreecommitdiff
path: root/src/librandom/random_uint32.c
blob: 26c0968019f998f6fbb6b6acda7d6d160603d73f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASARC4RANDOM

#include <skalibs/nonposix.h>
#include <stdint.h>
#include <stdlib.h>
#include <skalibs/random.h>

uint32_t random_uint32 (uint32_t n)
{
  return arc4random_uniform(n) ;
}

#else

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

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

#endif