commit a2e1a48d82dcba2583c0cd6e133d1a399fc2dcb1
parent 58cfe378e51797098a5f4f3082e4893b3550a7cf
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 3 Jun 2023 03:38:40 -0600
tweak: add xorshift example
Diffstat:
3 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/C/scratchpad/makefile b/C/scratchpad/makefile
@@ -0,0 +1,2 @@
+build:
+ gcc xorshift.c -o xorshift
diff --git a/C/scratchpad/xorshift b/C/scratchpad/xorshift
Binary files differ.
diff --git a/C/scratchpad/xorshift.c b/C/scratchpad/xorshift.c
@@ -0,0 +1,35 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+}