Thread: Random Walk Simulation in 1D for 10 or more particles

  1. #1
    Registered User
    Join Date
    Dec 2010

    Question Random Walk Simulation in 1D for 10 or more particles


    I have written a code for Random Walk in 1D for 1 particle. And I calculated its Mean square Density for 1 particle. Now I am trying to calculate the MSD for 10 particles, but I get something wrong. The value of MSD should increase when the particles are added. But unfortunately, I get almost the same numbers. Can anyone please have a look and see what is wrong with the code? here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #define N 100  /*flipping 100 times*/
    #define T 10 /*for 10 particles*/
    double rndm(void)
    /*Function to simulate random walk in 1D*/
    double walk(int start)
        double a;
        int i,x;
        for(i=0; i<N; i++)
            if(a<0.5)    /*Choses which direction to move in*/
    	return x; 
    /*Main Function*/
    int main () {
        FILE *fout;
        int i,j,start,random[N],x;
        double m;
        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
    	fprintf(fout, "%d\n", random[i]); // Writes Number To File
    	m+=(random[i]-start)*(random[i]-start); /*MSD for 10 particles*/
    	fprintf(fout, "The MSD for %d particle(s) is: %lf\n",j,m);
    	double p = 0;
        for (i=0; i<N; i++){
        fprintf(fout, "The MSD for 1 particle: %lf\n",p); /*Here I calculate MSD for 1 particle*/
    	return 0;  

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Perhaps you're starting with garbage...
    $ gcc -Wall -Wextra -O2 bar.c
    bar.c: In function ‘main’:
    bar.c:49: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    bar.c:61: warning: array subscript is above array bounds
    bar.c:43: warning: ‘m’ may be used uninitialized in this function
    The MSD for 10 particle(s) is: -171906769.316143
    The MSD for 1 particle: 101.160000
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Walk Simulation in 1D - Undesired output
    By SalamuB in forum C Programming
    Replies: 2
    Last Post: 01-30-2012, 07:03 AM
  2. Using 2 sets of arrays for a random walk 2D program
    By sashadem in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 01:48 AM
  3. Random walk solution
    By skiabox in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 10:36 AM
  4. Code for random walk in 1D
    By LLcoolc++ in forum C Programming
    Replies: 5
    Last Post: 10-18-2009, 09:33 AM
  5. Help with a random walk
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 02-27-2004, 09:11 PM