1 0fc65b37 2004-03-21 devnull #include <u.h>
2 0fc65b37 2004-03-21 devnull #include <libc.h>
3 0fc65b37 2004-03-21 devnull #include <libsec.h>
5 0fc65b37 2004-03-21 devnull #define Maxrand ((1UL<<31)-1)
8 0fc65b37 2004-03-21 devnull nfastrand(ulong n)
10 0fc65b37 2004-03-21 devnull ulong m, r;
13 0fc65b37 2004-03-21 devnull * set m to the maximum multiple of n <= 2^31-1
14 0fc65b37 2004-03-21 devnull * so we want a random number < m.
16 0fc65b37 2004-03-21 devnull if(n > Maxrand)
17 0fc65b37 2004-03-21 devnull sysfatal("nfastrand: n too large");
19 0fc65b37 2004-03-21 devnull m = Maxrand - Maxrand % n;
20 0fc65b37 2004-03-21 devnull while((r = fastrand()) >= m)
22 0fc65b37 2004-03-21 devnull return r%n;