ref: 6bda784cb6a0a933a3c3bde7e6b6d60d221d88f2
dir: /sys/src/libsec/port/nfastrand.c/
#include "os.h"
#include <libsec.h>
#define Maxrand ((1UL<<31)-1)
ulong
nfastrand(ulong n)
{
ulong m, r;
/*
* set m to the maximum multiple of n <= 2^31-1
* so we want a random number < m.
*/
if(n > Maxrand)
sysfatal("nfastrand: n too large");
m = Maxrand - Maxrand % n;
while((r = fastrand()) >= m)
;
return r%n;
}