diff --git a/C/scratchpad/makefile b/C/scratchpad/makefile new file mode 100644 index 00000000..819c0de8 --- /dev/null +++ b/C/scratchpad/makefile @@ -0,0 +1,2 @@ +build: + gcc xorshift.c -o xorshift diff --git a/C/scratchpad/xorshift b/C/scratchpad/xorshift new file mode 100755 index 00000000..bb1d7a19 Binary files /dev/null and b/C/scratchpad/xorshift differ diff --git a/C/scratchpad/xorshift.c b/C/scratchpad/xorshift.c new file mode 100644 index 00000000..5457e6fa --- /dev/null +++ b/C/scratchpad/xorshift.c @@ -0,0 +1,35 @@ +#include +#include +#include + +struct xorshift32_state { + uint32_t a; +}; + +uint32_t xorshift32(struct xorshift32_state *state) +{ + /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ + uint32_t x = state->a; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return state->a = x; +} + +int main(){ + struct xorshift32_state** state = malloc(sizeof(struct xorshift32_state*) * 4); + for(int i=0; i<4;i++){ + state[i] = malloc(sizeof(struct xorshift32_state)); + state[i]->a = (uint32_t) i + 1; + } + + printf("%i\n", xorshift32(state[0])); + printf("%i\n", xorshift32(state[0])); + for(int i=0; i<4;i++){ + free(state[i]); + } + free(state); + + + return 0; +}