commit 00684179e11c26cb02cba9e9b15faa7b1a8f4ca6
parent 131ea138ae09e609d5bf01daf3c5bceafa0a9981
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 3 Jun 2023 03:47:10 -0600
add 0 to 1 float to xorshift implementation
Diffstat:
2 files changed, 12 insertions(+), 3 deletions(-)
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
@@ -12,15 +12,21 @@ uint32_t xorshift32(uint32_t* state)
return *state = x;
}
+float rand_xorshift32(uint32_t* state){
+ return (float) xorshift32(state) / UINT32_MAX;
+}
+
int main(){
uint32_t** states = malloc(4 * sizeof(uint32_t*));
for(int i=0; i<4;i++){
states[i] = malloc(sizeof(uint32_t));
*states[i] = i + 1;
}
-
- printf("%i\n", xorshift32(states[0]));
- printf("%i\n", xorshift32(states[1]));
+
+ for(int i=0; i<100; i++){
+ printf("%u\n", xorshift32(states[0]));
+ printf("%f\n", rand_xorshift32(states[1]));
+ }
for(int i=0; i<4;i++){
free(states[i]);
}
@@ -28,3 +34,6 @@ int main(){
return 0;
}
+
+// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>
+// https://en.wikipedia.org/wiki/Xorshift