I am trying to implement a Fibonacci delayed pseudo random number generator for integers to a certain maximum. It supports an array of values.
int values[SIZE] = { };
and uses the following function to return the next value
unsigned lagfib()
{
static unsigned idx = 0;
int r = values[idx];
values[idx] = (values[(idx-24) % SIZE] + values[(idx-55) % SIZE])
% MAX_VALUE;
idx = (idx+1) % SIZE;
return r;
}
Essentially, it valuesshould be a simple ring buffer that is always full. Subtraction and modulo should wrap the index at the end of the array. SIZEthere should always be at least 55, but I want to round to 64 to speed up the work modulo.
But, apparently, I have incorrect modulo calculations, and I donβt know how to fix them. Changing the index type to intdoes not improve the situation.
(PS.: , static , , C ++, .)