tweak: add xorshift example
This commit is contained in:
parent
58cfe378e5
commit
a2e1a48d82
2
C/scratchpad/makefile
Normal file
2
C/scratchpad/makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
build:
|
||||
gcc xorshift.c -o xorshift
|
BIN
C/scratchpad/xorshift
Executable file
BIN
C/scratchpad/xorshift
Executable file
Binary file not shown.
35
C/scratchpad/xorshift.c
Normal file
35
C/scratchpad/xorshift.c
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user