Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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