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/ape/lib/v/rand.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
#define _RESEARCH_SOURCE
2
#include <stdlib.h>
3
#include <libv.h>
4
/*
5
	random number generator from cacm 31 10, oct 88
6
	for 32 bit integers (called long here)
7
*/
8
 
9
#ifdef	MAIN
10
#define	A	16807
11
#define	M	2147483647
12
#define	Q	127773
13
#define	R	2836
14
#else
15
#define	A	48271
16
#define	M	2147483647
17
#define	Q	44488
18
#define	R	3399
19
#endif
20
 
21
static long seed = 1;
22
 
23
void
24
srand(unsigned int newseed)
25
{
26
	seed = newseed;
27
}
28
 
29
long
30
lrand(void)
31
{
32
	long lo, hi, test;
33
 
34
	hi = seed/Q;
35
	lo = seed%Q;
36
	test = A*lo - R*hi;
37
	if(test > 0)
38
		seed = test;
39
	else
40
		seed = test+M;
41
	return(seed);
42
}
43
 
44
int
45
rand(void)
46
{
47
	return lrand()%(RAND_MAX+1);
48
}
49
 
50
#ifdef	MAIN
51
 
52
main()
53
{
54
	int i;
55
 
56
	for(i = 0; i < 10000; i++)
57
		rand();
58
	if(seed == 1043618065)
59
		printf("     rand: pass\n");
60
	else
61
		printf("*****rand: fail; seed=%u, should be 1043618065\n", seed);
62
	exit(0);
63
}
64
#endif