]> git.ozlabs.org Git - ppp.git/blob - NeXT/random.c
updated for 2.3.2
[ppp.git] / NeXT / random.c
1 /*
2  * Because the standard library random number
3  * functions are not availble at the kernel level
4  * I wrote this simple random number generator.
5  *
6  * It uses the multiplicative congruential method.
7  * See pg 263 of Banks and Carson "Discrete-Event
8  * System Simulation".
9  *
10  */
11
12 #include "random.h"
13
14 static unsigned x0=123457;     /* seed */
15 static unsigned a=16807;       /* constant multiplier */
16 static unsigned c=0;           /* increment */
17 static unsigned m=2147483647;  /* modulus */
18
19 /*
20  * Set the seed to the argument.
21  */
22
23 void srand(unsigned i)
24 {
25   x0 = i;
26 }
27
28
29 /*
30  * Use Linear Congruential Method to Generate
31  * sequence.  Return either int or float...
32  */
33   
34 unsigned rand(void)
35 {
36   unsigned tmpseed;
37
38   tmpseed = (a*x0+c) % m;
39   x0 = tmpseed;
40   return (unsigned) x0;
41 }
42
43
44
45 float frand(void)
46 {
47   unsigned tmpseed;
48
49   tmpseed = (a*x0+c) % m;
50   x0 = tmpseed;
51   return (x0/(float)m);
52 }