Reputation: 5940
I was given a .C file which i don't know what it does and I'm supposed to find out... after running it i think it is some sort of random number generator but i totally have no idea... i was wandering if anyone one of you has ever seen the algorithm implemented in the rf function:
/* ===========================================HEADERS=============================================== */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* ============================================INIRAN================================================ */
typedef struct {
int seme;
} iniran;
/* ==============================================RF================================================== */
float rf(int *idum){
static int iff=0;
static int inext, inextp, ma[55];
int mj, mk;
int i, k, ii;
float ret_val;
if (*idum<0 || iff==0) {
iff=1;
mj=161803398 - abs(*idum);
mj %= 1000000000;
ma[54]=mj;
mk=1;
for (i=1; i<=54; ++i){
ii=(i*21)%55;
ma[ii-1]=mk;
mk=mj-mk;
if (mk<0) {
mk += 1000000000;
}
mj= ma[ii-1];
}
for(k=1; k<=4; ++k) {
for(i=1; i<=55; ++i){
ma[i-1] -= ma[(i+30)%55];
if (ma[i-1]<0){
ma[i-1] += 1000000000;
}
}
}
inext=0;
inextp=31;
*idum=1;
}
++inext;
if (inext==56){
inext=1;
}
++inextp;
if (inextp==56){
inextp=1;
}
mj=ma[inext-1]-ma[inextp-1];
if (mj<0){
mj += 1000000000;
}
ma[inext-1]=mj;
ret_val=mj*1.0000000000000001e-9;
return ret_val;
}
/* ============================================MAIN================================================ */
int main(void){
/* Variable declaration */
int Np=10000;
int jp1, jp2, jp3, jp4, jp5;
/* Rinominate struct */
iniran iniran1;
iniran1.seme=7593;
jp1=1+floorf(Np*rf(&iniran1.seme));
jp2=1+floorf(Np*rf(&iniran1.seme));
jp3=1+floorf(Np*rf(&iniran1.seme));
jp4=1+floorf(Np*rf(&iniran1.seme));
jp5=1+floorf(Np*rf(&iniran1.seme));
printf("jp1 = %d, jp2 = %d, jp3 = %d, jp4 = %d, jp5 = %d \n", jp1, jp2, jp3, jp4, jp5);
return 0;
}
In order to run it you must type gcc test.c -lm
in the terminal. I know there's little information but maybe you have seen this kind of algorithm before and you might help me to understand what it is going on... any suggestion is welcome
Upvotes: 0
Views: 107
Reputation: 94319
Just searching for the code on Google yields e.g. this question which uses similiar variable names and constants (note e.g. 161803398 or the array ma
with 55 elements). In fact (my Fortran is kinda rusty) it appears to be the exact same algorithm -- and that question is about a random number generator.
Some more research suggests that this is a C implementation of Knuth's subtractive random number generator algorithm.
Upvotes: 6