hi,
Im trying to make a file with the results of the final position of 1000 1d random walks printed on it but at the moment my file seems to give the same random value 1000 times, can anyone see anyproblems with my code??
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 1000
double rndm(void)
{
return(double)rand()/((double)RAND_MAX);
}
/*Function to simulate random walk in 1D*/
double walk(int start)
{
double a;
int i,x;
srand(time(NULL));
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
}
fclose(fout); // Closes The File
return 0;
}
Thanks!