-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMM.c
53 lines (39 loc) · 1.14 KB
/
MM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Mitchell-Moore algorithm from
* The Art of Computer Programming, Volume II, by Donald Knuth */
#include "generators.h"
#define L 37U
#define K 100U
static unsigned long int sequence[K];
static unsigned int b = L-1, a = K-1;
void init_mm(unsigned long int seed) {
unsigned int i;
for (i = 0; i < K * 2; i++)
sequence[i % K] = seed = (1664525 * seed + 1013904223);
return;
}
/* This way I export one of the implementations below */
static inline unsigned long int rand_mm1(void);
static inline unsigned long int rand_mm2(void);
static inline unsigned long int rand_mm3(void);
unsigned long int rand_mm(void) {
return rand_mm3();
}
/* Implementations of the algorithm: */
/* Clear version */
static inline unsigned long int rand_mm1(void) {
if (a == K)
a = 0;
else if (b == K)
b = 0;
return sequence[a++] += sequence[b++];
}
/* Alternative clear version */
static inline unsigned long int rand_mm2(void) {
a++;
b++;
return sequence[a % K] += sequence[b % K];
}
/* Faster version */
static inline unsigned long int rand_mm3(void) {
return sequence[a = (a == K-1) ? 0 : a + 1] += sequence[b = (b == K-1) ? 0 : b + 1];
}