Thread: Segmantation fault - RandomWalk 1D

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    20

    Question Segmantation fault - RandomWalk 1D

    Hi,

    I have a code written for simulation of Random Walk in 1D and calculation of its Mean Square Displacement. It does compile, but for some reason, it gives me segmantation fault. I really don't get what is wrong as it seems right to me. Can anyone please have a look and help me to find a bug? Thank you.

    Here is the code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    #include <float.h>
    #include <limits.h>
    #include <sys/types.h>
    #include <sys/times.h>
    
    #define T 1000 /* number of time steps */
    #define N 100  /* number of "particles" */
    
    
    double rndm(void){
      return(double)rand()/RAND_MAX;
    }
    
    
    /*Function to increase/decrease position by one in 1D*/
    double walk(int start){
    	
      double a;
      int i,x;
    	
      x=start;
      a=rndm();
      if(a<0.5){    /*Choses which direction to move in*/
        x++;
      }
      else{ 
        x--;
      }
    
      return x; 
    
    }
    
    
    int main()
    {
      FILE *myfilename;
      int i,t;
      int random[N];
      double m;
      int xdummy=0,flag=100;
    
      srand(time(NULL));
    
      
      if((myfilename=fopen("RandomWalk.dat","w"))==NULL){
        printf("It didn't succeed to open 'RandomWalk.dat' file.\n");
        printf("Exiting...\n");
        exit(0);
      }
      fprintf(myfilename,"#Time| MSD\n");
      fprintf(myfilename,"#=========\n");
      
      printf("#Time| MSD\n");
      printf("#=====================\n");
    
      /*setup initial configuration (start @ zero)*/
      for(i=1;i<=N;i++){
        random[i]=0;
      }
    
      for(t=1;t<=T;t++){
        xdummy++;
        m=0.0;
        for(i=1;i<=N;i++){
          random[i]=walk(random[i]);
          /*printf("random[%d]= %d\n",i,random[i]);*/
    	fprintf(myfilename, "random[%d]= %d\n",i,random[i]);
          m+=(random[i]*random[i]);
          /*printf("m= %lf\n",m);*/
    	fprintf(myfilename,"m= %lf\n",m);
    
        }
        if(xdummy==flag){
          m=m/((double)(N));
          /*printf("t= %d, MSD= %lf\n",t,m);*/
          printf(" %5d  %10.5lf\n",t,m);
          fprintf(myfilename,"%5d  %lf\n",t,m);
          xdummy=0;
        }
      }
    
      fclose(myfilename);
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    elaborate your problem

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    When I print the output only to screen, it works fine, no problem with it, but when I include the statements to output to the File, it gives me segmentation fault. Something wrong with printing the output to the file RandomWalk.dat.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One problem is you consistently use the wrong limits on your for loops:
    Code:
      for(i=1;i<=N;i++){
        //etc.
      }
    correct is:
    Code:
    for(i = 0;i < N; i++) // start with 0 index, stop at N-1, not N
    Also, you have the multiplication of two integers, being assigned m (a double). Since when does multiplying two integers result in anything other than an integer?

    Since you may have large int's to deal with, you may want to use unsigned long as your data type, but a double is a whole 'nother kettle of fish, imo.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    Great, it worked! Thank you a lot, Adak !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-03-2012, 12:18 PM
  2. Replies: 7
    Last Post: 12-08-2010, 04:36 PM
  3. strcpy(), 2 strings, and segmantation fault
    By jigidyjensen in forum C Programming
    Replies: 4
    Last Post: 03-20-2009, 01:42 PM
  4. compare segmantation fault
    By realcr in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2006, 05:56 PM
  5. Segmantation Fault (core dumped) error
    By Vinnie66 in forum C Programming
    Replies: 6
    Last Post: 03-25-2002, 01:34 PM