blob: dfa97834be3bd0f43c8d0c1f1bc74d6ccbdd4fcc (
plain)
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
|
// https://en.wikipedia.org/wiki/Linear_congruential_generator -> MMIX Donald Knuth
// modulo 2^64 = no need to do it explicitly
#include "types.h"
#define MULTIPLIER 6364136223846793005LL
#define INCREMENT 1442695040888963407LL
static uint64_t current;
void srand(uint64_t seed){
seed = current;
}
uint64_t randlong(void){
return (current = MULTIPLIER * current + INCREMENT);
}
#ifdef __UNIT_TEST_PRNG__
char bytewise_sum(uint64_t to_check){
char sum = 0;
for (int i = 0 ; i < 8 ; i++)
sum += (to_check & (uint64_t)(0xFFULL << i*8)) >> i*8;
return sum;
}
int main(void){
srand(42);
if (bytewise_sum(0xdeadbeefb00b1355ULL) != 91)
return 1;
for (int i = 0 ; i < 1000 ; i++)
randlong();
uint64_t last = randlong();
return !((unsigned char)bytewise_sum(last) == 251);
}
#endif // __UNIT_TEST_PRNG__
|