What algorithm has been implemented to generate random numbers in the following code?
unlong M=714025, IA=1366, IC=150889;
void igenran(unlong idum, int *G, int K) {
int i;
for (i = 1; i <= K; i++) {
idum = (IA * idum + IC) % M;
G[i] = idum * 36451;
}
}
The main code is:
int main() {
unlong idum;
int irh[4];
for (int i = 1; i <= 10; i++){
igenran(idum, irh, 4);
printf("%d %d\n", i, irh[3]);
}
}
How does it work? Clearly, idum is undefined, so what value is being passed to the function? Why does this value differ with each pass to the function? Also, why is the for loop within igenran running from 1 to 4 when the array is indexed from 0 to 3?
Aucun commentaire:
Enregistrer un commentaire