Hello Guys, I am struggling with the code for simulation of Random Walk in 1D. It compiles, but the output to text file "Walk.txt" are all almost the same negative numbers. BTW I am a new to C programming, if you could look at the code and give your ideas about what is wrong with it. Here is the code itself:

In this part, i've tried to calculate the mean square displacement (position):Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define N 10 double rndm(void) { return(double)rand()/RAND_MAX; } /*Function to simulate random walk in 1D*/ double walk(int start) { double a; int i,x; x=start; for(i=0; i<N; i++) { a=rndm(); if(a<0.5) { /*Choses which direction to move in*/ (x=x+1); } if (a>0.5) { (x=x-1); } } return x; } /*Main Function*/ int main () { FILE *fout; int i,start,random[N],x; srand(time(NULL)); fout=fopen("walk.txt", "w"); //Open The File printf("What would you like the initial x position to be?\n:"); scanf("%d", &start); //Define Start position for x for(i=0; i<N; i++) { x=walk(start); // Generates Random Number random[i]=x; fprintf(fout, "%d\n", random[i]); // Writes Number To File } double p = 0; for (i=0; i<N; i++){ p+=(random[i]-start)*(random[i]-start);} p=p/N; fprintf(fout, "%lf\n",p); fclose(fout); return 0; }

And the output in "Walk.txt" is :Code:double p = 0; for (i=0; i<N; i++){ p+=(random[i]-start)*(random[i]-start);} p=p/N; fprintf(fout, "%lf\n",p); fclose(fout); return 0; }

Code:-1079135414 -1079135418 -1079135418 -1079135406 -1079135418 -1079135416 -1079135418 -1079135416 -1079135414 -1079135416 12.400000