PatchworkOS
Loading...
Searching...
No Matches
random.c
Go to the documentation of this file.
1#include "random.h"
2
3#include <stdint.h>
4#include <stdlib.h>
5
6static uint64_t seed = 0;
7
8int random_gen(void)
9{
10 uint64_t oldstate = seed;
11
12 seed = oldstate * 6364136223846793005ULL + 1442695040888963407ULL;
13
14 uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
15 uint32_t rot = oldstate >> 59u;
16 uint32_t result = (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
17
18 return (int)(result & RAND_MAX);
19}
20
21void random_seed(unsigned newSeed)
22{
23 seed = newSeed * 6364136223846793005ULL + 1442695040888963407ULL;
24}
static uint64_t seed
Definition random.c:6
int random_gen(void)
Definition random.c:8
void random_seed(unsigned newSeed)
Definition random.c:21
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define RAND_MAX
Definition stdlib.h:36