-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.c
More file actions
31 lines (23 loc) · 687 Bytes
/
Copy pathrandom.c
File metadata and controls
31 lines (23 loc) · 687 Bytes
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
#include "random.h"
#include <inttypes.h>
// pro tip: never code your own random number generator.
// always copy someone else
uint64_t keys[2];
inline uint64_t rotate(uint64_t v, uint8_t s) {
return (v >> s) | (v << (64 - s));
}
inline uint64_t RandomUInt64() {
uint64_t tmp = keys[0];
keys[0] += rotate(keys[1] ^ 0xc5462216u ^ ((uint64_t) 0xcf14f4ebu << 32), 1);
return keys[1] += rotate(tmp ^ 0x75ecfc58u ^ ((uint64_t) 0x9576080cu << 32), 9);
}
void SeedRandom(uint64_t seed) {
keys[0] = seed;
keys[1] = seed;
for (int i = 0; i < 64; i++) {
RandomUInt64();
}
}
uint64_t RandomMagic() {
return RandomUInt64() & RandomUInt64() & RandomUInt64();
}