Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_posix/sys/src/cmd/unix/drawterm/libsec/nfastrand.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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