What I'm trying to do is generating some random numbers (-1<x<1) and then saving them in the array. p[i] = (double)2*rand()/RAND_MAX-1;
So far there's no error (random numbers were reasonably generated), but problem occurred when I tried to get values from the array. q += p[i]*p[j]
The array p[j] gives strange values like 0 (yes there can be zero, but it looks strange that array gives 0 continuously) or very big value like 28040310013302517619982145582267442588916597218393832117220025500645045329090733358176461602261387426622486260295380601312859137588835453228348262580224.000000. Not always, but sometimes.
I think there should be no error. I've checked that both indices i and j are inside the array range. Did I make any mistakes? I've attached my code below. (My code is calculating a totally meaningless thing, so you don't have to care about that.)
Thanks!
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *file;
file = fopen("result.txt","w");
int N = 10000;
double p[N]={0};
for(int i=0;i<N;i++){
p[i] = (double)2*rand()/RAND_MAX-1; //range[-1, 1]
}
int j;
double x,y;
for(int n=-100; n<=100; n++){
double q = 0;
for(int i=0; i<N; i++){
j = (i+n)%N;
q += p[i]*p[j];
}
fprintf(file,"%d\t%f\n",n,q);
}
fclose(file);
}