-
Notifications
You must be signed in to change notification settings - Fork 325
/
LinearCongruentialGenerator.c
37 lines (32 loc) · 930 Bytes
/
LinearCongruentialGenerator.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
// A basic implementation of a linear congruential pseudorandom number generator in C
#include <stdio.h>
#include <time.h>
#include <math.h>
struct lcg_rand {
unsigned int modulus;
unsigned int multiplier;
unsigned int increment;
unsigned int seed;
unsigned int last;
};
struct lcg_rand new_generator(unsigned int modulus, unsigned int multiplier, unsigned int increment) {
struct lcg_rand random;
random.modulus = modulus;
random.multiplier = multiplier;
random.increment = increment;
random.seed = time(NULL);
random.last = random.seed;
return random;
}
int random(struct lcg_rand * random) {
int number = ((random->multiplier * random->last) + random->increment) % random->modulus;
random->last = number;
return number;
}
int main() {
struct lcg_rand rng = new_generator(pow(2, 31), 1103515245, 12345);
for (int i = 0; i < 10; i++) {
printf("%d\n", random( & rng));
}
return 0;
}