I'm very new to C. I want to write C-code that I will wrap in Cython.
This is my C-code (a random-value generator based on a seed):
#include <math.h>
typedef struct {
int seed;
int bits;
int max;
double fmax;
int value;
} rGen;
rGen rGen_Init(int seed, int start, int bits) {
rGen gen;
int max = pow(2, bits);
gen.seed = seed % max;
gen.bits = bits;
gen.max = max;
gen.fmax = (double) max;
gen.value = start % max;
return gen;
}
double rGen_Next(rGen gen) {
int value = gen.value;
value = (gen.seed << gen.bits) * value / (gen.bits + 1);
value = pow(value, 4);
value = value % (gen.max - 1);
gen.value = value;
return (double) value / gen.fmax;
}
Doing a little check if the value
attribute of the rGen instance does change:
#include "pyran.c"
#include <stdio.h>
int main() {
rGen gen = rGen_Init(1000, 200, 32); // Create an initialized struct-instance
int i;
for(i = 0; i < 10; i += 1) {
rGen_Next(gen);
printf("%i\n", gen.value);
}
return 0;
}
The output is:
200
200
200
200
200
200
200
200
200
200
The value of the value
attribute does not change.
Why does it behave like this ? Do I have to use pointers ? And if so, how do I use them ?