【问题标题】:Random walk simulation in one dimension using random number generator使用随机数生成器进行一维随机游走模拟
【发布时间】:2020-07-25 22:15:28
【问题描述】:

我正在尝试开发一维随机游走模拟。我正在沿着一条具有 10 个粒子可以占据的离散位置的线对粒子进行建模。粒子每次“跳跃”时只能向左或向右移动一格。在这个模拟中,我得到了解释 20 跳的代码。在执行每个“跳跃”以告诉“粒子”向左或向右移动之前,随机数生成器会产生 0 - 代表左侧和 1 - 代表右侧。请参阅下面的代码和进一步的 cmets。

#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);
}

每次我的输出都不是我所期望的那种模式。例如,它似乎输出粒子在一次迭代中处于位置 0,而在下一次迭代中突然处于位置 4。我宁愿打印站点 [] 数组的条目,而不是在每个条目中输入位置并打印指针的值。

如果有人在这里提供帮助,我将不胜感激。我是指针的新手,所以任何帮助将不胜感激。

【问题讨论】:

  • 请重新表述您的问题,使其更简洁、更通用,以便更容易回答,并造福后代
  • time()的返回类型是time_t。你的int 可能太小了。这也意味着:打开编译器的警告!

标签: c arrays pointers random-seed random-walk


【解决方案1】:

考虑以下部分:

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

现在,考虑 for 循环何时执行第一次迭代。 如果方向是 0 怎么办? location 将变为 &amp;sites[0] - 1,当然它会超出数组。 所以,你应该为数组sites设置边界检查条件。

【讨论】:

    【解决方案2】:

    这解决了我遇到的问题。我决定通过 for 循环用与位置相对应的整数填充元素来自动化指示粒子位置的数组。如果有任何关于如何使该程序更高效或更易于阅读的建议,请不要犹豫发表评论。

    #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);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多