0

I am trying to develop a random walk simulation in one dimension. I am modelling a particle along a line with 10 discrete positions that particle can occupy. The particle can only move left or right one space each time it 'hops'. In this simulation I get the code to account for 20 hops. A random number generator produces a 0 - for left and 1 - for right before each 'hop' is executed to tell the "particle" to go left or right. Please see code below and further comments.

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>

int randomInt( int max); // declaration of function //

int main() 
{ // declaration of variables //
  int i, j = 0;
  int totalHops=20; 
  int seed;
  int sites[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
  int *location;
  location = &sites[j];

  seed = time(NULL); 

  printf("The seed value for this 'random walk' is: %d.\n\n", seed); //keeping track of the seed used// 

  // setup the random number generator // 
  srandom(seed);  // random path sequence //
  printf("Initial location of the particle is at site %d.\n\n", *location);
  for ( i=1; i<totalHops+1; i++ ) 
  {

    int direction = randomInt(2); // Two possible directions for particle to move, 0 = left, 1 = right // 

    if (direction == 1) {
        location+= 1; // pointer moves right one space on array //
    }
    else {
        location-= 1; // pointer moves left one space on array // 
    }

    printf("After %2.d hop(s), the particle is at site %d.\n", i, (*location)%10);  // I  would prefer to be printing the entry of my array rather than relying on the fact the array is lablled with each entry the positon I have changed the pointer to //
  }
  printf("\n");
}

// function definition //
int randomInt(int max)
{
    return (random() % max);
}

My output does not follow each time the sort of patterns I am expecting. It appears to output that the particle is in postion 0 in one iteration and in the next all of a sudden be in postion 4, as an example. I would prefer that I were printing the entry of the sites[] array instead of inputing the postion into each entry and printing the value of the pointer.

I would be most grateful for anyones help here. I am new to pointers so any help would be much appreciated.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
adamlsmith981
  • 63
  • 1
  • 8
  • 1
    Please rephrase your question so it's minimal and more generic, so it'd be easier to answer AND will benefit future generations – GalAbra Apr 13 '20 at 11:09
  • 1
    The return type of `time()` is `time_t`. Your `int` may be too small. It also means: _turn warnings of your compiler on!_ – Paul Ogilvie Apr 13 '20 at 11:21

2 Answers2

2

Consider following part:

if (direction == 1) {
        location+= 1; // pointer moves right one space on array //
    }
else {
    location-= 1; // pointer moves left one space on array // 
}

Now, consider when the for loop executing first iteration. What if direction is 0 ? The location will become &sites[0] - 1, off course it will out of array. So, you should put conditions for boundary checking for array sites.

ravivasani75
  • 113
  • 6
0

This fixes the probelm I was having. I decided to automate the array that indicates the position of the particle by filling up the elements with an integer corresponding to the position via a for loop. If there are any recommendatons on how to make this program more efficient or easier to read dont hesitate to comment.

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
int randomInt( int max);
int main() 
{ 
  int i, j = 0;
  const int L = 10;
  const int totalHops=20; 
  int sites[L] = {};
  for (int k=0; k<L; k++) 
  {sites[k]=k+1;} 
  int *location;
  location = &sites[j];
  srandom(time(NULL));  
  printf("\nThe seed value for this 'random walk' is: %ld.\n\n", time(NULL)); 
  printf("Initial location of the particle is at site %d.\n\n", *location);
    tracker[0] = sites[0];
  for ( i=1; i<=totalHops; i++ ) 
  { 
    int direction = randomInt(2); 
    // Two possible directions for particle to move, 0 = left, 1 = right // 
    if (direction == 1) {
        location+= 1; // pointer moves right one space on array //
        if (location == &sites[L-1]+1) // takes particle from site 10 to 1 //
        {
            location = &sites[0];
        }
    }   else if (direction == 0) {
        location-= 1; // pointer moves left one space on array //
        if (location == &sites[0] - 1) // takes particle from site 1 to 10 //
        {
            location = &sites[L-1]; 
        }  
    }
    printf("After %2.d hop(s), the particle is at site %d.\n", i, (*location));
  } printf("\n");
}
// function definition //
int randomInt(int max)
{
    return (random() % max);
}
adamlsmith981
  • 63
  • 1
  • 8