J
Joseph Wakeling
Hello all,
Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.
/*************************************************************/
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 = s;
}
x = s;
n = s;
return;
}
/*************************************************************/
Anyway, two questions:
(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)
(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?
Many thanks,
-- Joe
Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.
/*************************************************************/
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 = s;
}
x = s;
n = s;
return;
}
/*************************************************************/
Anyway, two questions:
(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)
(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?
Many thanks,
-- Joe