9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

nrand.c (176B)


      1 #include <lib9.h>
      2 
      3 #define	MASK	0x7fffffffL
      4 
      5 int
      6 nrand(int n)
      7 {
      8 	long slop, v;
      9 
     10 	if(n < 0)
     11 		return n;
     12 	slop = MASK % n;
     13 	do
     14 		v = lrand();
     15 	while(v <= slop);
     16 	return v % n;
     17 }