3 /* Pseudo-random generator based on Minimal Standard by
4 Lewis, Goodman, and Miller in 1969.
6 I[j+1] = a*I[j] (mod m)
11 Using Schrage's algorithm, a*I[j] (mod m) can be rewritten as:
13 a*(I[j] mod q) - r*{I[j]/q} if >= 0
14 a*(I[j] mod q) - r*{I[j]/q} + m otherwise
16 where: {} denotes integer division
20 note that the seed value of 0 cannot be used in the calculation as
21 it results in 0 itself
25 rand_r (unsigned int *seed
)
28 long s
= (long)(*seed
);
32 s
= 16807 * (s
- k
* 127773) - 2836 * k
;
35 (*seed
) = (unsigned int)s
;
36 return (int)(s
& RAND_MAX
);