1 #include "os.h" 2 #include <libsec.h> 3 4 #define Maxrand ((1UL<<31)-1) 5 6 ulong 7 nfastrand(ulong n) 8 { 9 ulong m, r; 10 11 /* 12 * set m to the maximum multiple of n <= 2^31-1 13 * so we want a random number < m. 14 */ 15 if(n > Maxrand) 16 sysfatal("nfastrand: n too large"); 17 18 m = Maxrand - Maxrand % n; 19 while((r = fastrand()) >= m) 20 ; 21 return r%n; 22 } 23