From 131ea138ae09e609d5bf01daf3c5bceafa0a9981 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 3 Jun 2023 03:42:30 -0600 Subject: [PATCH] simplify xorshift implementation; struct not needed. --- C/scratchpad/xorshift | Bin 16824 -> 16824 bytes C/scratchpad/xorshift.c | 25 ++++++++++--------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/C/scratchpad/xorshift b/C/scratchpad/xorshift index bb1d7a19161538531cf21bebecdf3598b94cfeff..552d370b684140678d6579066ee7c7495923db6d 100755 GIT binary patch delta 183 zcmdnd%($bOaYF{P$m-A@=ZuAmdBuO2PM9UF{`}R$32B>an5Xi|xIX9&VC;5%;4#Ca zm**%05O{REzVKi?;nB@7Sy#wdzWD$Lkm=ER{Dt)Y|NlFWdGy-$@-i^&04aU(|L^2# zAvMk8fB*k)c74LwT>FH%?mE~2p!$tKQLr+P<|7i(v4{EPT{fQ+vf~qCWCZGUkALY6CT|R&4(B#>k1i5cQbf&9)BVK|NsBaV;;S>6L}dJc7RmAVE#9`UPw*% z^xyygn_ZtUHrGC3uDcG_4^+PuC<<2Q(R@TAI`(iHzkCZ&)8=zRc6>sALHb?!1lpLK ZdD&c>nVH^imQyqmW%|poImj%H8vqX^M@#?! diff --git a/C/scratchpad/xorshift.c b/C/scratchpad/xorshift.c index 5457e6fa..e3b8ee81 100644 --- a/C/scratchpad/xorshift.c +++ b/C/scratchpad/xorshift.c @@ -2,34 +2,29 @@ #include #include -struct xorshift32_state { - uint32_t a; -}; - -uint32_t xorshift32(struct xorshift32_state *state) +uint32_t xorshift32(uint32_t* state) { /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ - uint32_t x = state->a; + uint32_t x = *state; x ^= x << 13; x ^= x >> 17; x ^= x << 5; - return state->a = x; + return *state = x; } int main(){ - struct xorshift32_state** state = malloc(sizeof(struct xorshift32_state*) * 4); + uint32_t** states = malloc(4 * sizeof(uint32_t*)); for(int i=0; i<4;i++){ - state[i] = malloc(sizeof(struct xorshift32_state)); - state[i]->a = (uint32_t) i + 1; + states[i] = malloc(sizeof(uint32_t)); + *states[i] = i + 1; } - printf("%i\n", xorshift32(state[0])); - printf("%i\n", xorshift32(state[0])); + printf("%i\n", xorshift32(states[0])); + printf("%i\n", xorshift32(states[1])); for(int i=0; i<4;i++){ - free(state[i]); + free(states[i]); } - free(state); - + free(states); return 0; }