blob: 9009a590f7c47401aa2f7f21c3a879e549480962 (
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
|
/* ISC license. */
#include <skalibs/sysdeps.h>
#ifdef SKALIBS_HASARC4RANDOM
#include <skalibs/nonposix.h>
#include <stdlib.h>
#include <skalibs/uint32.h>
#include <skalibs/random.h>
uint32 random_uint32 (uint32 n)
{
return arc4random_uniform(n) ;
}
#else
#include <skalibs/uint32.h>
#include <skalibs/random.h>
uint32 random_uint32 (uint32 n)
{
uint32 min, x ;
if (n < 2) return 0 ;
min = -n % n ;
for (;;)
{
char tmp[4] ;
random_string(tmp, 4) ;
uint32_unpack(tmp, &x) ;
if (x >= min) break ;
}
return x % n ;
}
#endif
|