Hello all,
I've been adapting some code from the GNU Scientific Library (GSL) for my own evil purposes. The following is a random number generator "ran1" (same algorithm as found also in Numerical Recipes for C):
Anyway, the main problem is that the compiler returns an error for the "static inline" bit before the function ran1_get(void). Any ideas why?
Second, what are the benefits of declaring functions as static and/or inline as is done here?
Third, why in the loops in ran1_set are the long int variables h and t redeclared each time round in the loop?
Many thanks,
-- Joe
I've been adapting some code from the GNU Scientific Library (GSL) for my own evil purposes. The following is a random number generator "ran1" (same algorithm as found also in Numerical Recipes for C):
Code:
#define N_SHUFFLE 32
#define N_DIV (1 + 2147483646/N_SHUFFLE)
static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
static const double z_max = 1 - 1.2e-7f;
static unsigned long int x;
static unsigned long int n;
static unsigned long int shuffle[N_SHUFFLE];
static inline unsigned long int ran1_get (void) {
const long int h = x / q;
const long int t = a * (x - h * q) - h * r;
if(t<0)
x = t + m;
else
x = t;
{
unsigned long int j = n / N_DIV;
n = shuffle[j];
shuffle[j] = x;
}
return(n);
}
static double ran1_get_double (void) {
double z = ran1_get() / 2147483647.0f ;
if(z > z_max)
return(z_max);
return(z);
}
static void ran1_set (unsigned long int s) {
int i;
if(s==0)
s = 1;
for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}
for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
}
x = s;
n = s;
return;
}
Anyway, the main problem is that the compiler returns an error for the "static inline" bit before the function ran1_get(void). Any ideas why?
Second, what are the benefits of declaring functions as static and/or inline as is done here?
Third, why in the loops in ran1_set are the long int variables h and t redeclared each time round in the loop?
Many thanks,
-- Joe