使用 Monte Carlo 算法计算热平衡分布的程序中 C++ 似乎跳过行的问题

Problem with C++ seemigly skipping lines in program for calculating thermal equilbirum distrobutions with Monte Carlo Alogrithim

我正在编写一段代码作为我的线性代数项目的一部分。它应该使用称为随机游走 属性 的 Monte Carlo 算法来计算处于静态热平衡状态的物体的热平衡分布。我是一名机械工程专业的学生,​​我从未学过计算机科学 class 所以代码中的所有内容都是自学的结果所以请原谅,因为我理解它是一个程序的科学怪人。

基本上,该程序在网格上获取一个指定点,并根据随机输入将其向左、向右、向上、向下移动,直到它到达该网格上的边界点,然后将它落在的数字扔到一个向量中并重复直到向量中值的数量等于预定义的迭代次数。该程序尚未完成,但我遇到的问题是我希望程序在达到边界点后 "reset" 回到起点。这似乎适用于数量或迭代次数较小(1-5)但对于更高的情况,因为程序会跳过该步骤...我让程序打印它在网格上的位置的 "coordinates"我的意图是它永远不会是不在网格内的数字,但当迭代很大时情况似乎并非如此。

#include <iostream>
#include <stdlib.h>
#include <array>
#include <algorithm>
#include <iterator>
#include <random>

using namespace std;

int main () {
int grid[5][5] = { //makes grid, this will be the model of the body that   we want to find temperatues for
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{1, 1, 1, 1, 1}
};
srand(time(NULL)); //seeds the random number generator
int x=2; // starting 'x' position 
int y=3; // starting 'y' position
vector<int> values; // vector that will store values of boundry points     once reached
int iterations =  7; // number of iterations
auto numberOfValues = values.size(); // how many numbers in the vector     (how many times has the program reached the boundry points)
while(numberOfValues != iterations) {
int randomPath = rand()%4; // get four random numbers to define to move left, right, up, or down


   switch (randomPath)
   {
       case 0:
       x++;
       cout <<"right"<<endl;
       break;
       case 1:
       x--;
       cout <<"left"<<endl;
       break;
       case 2:
       y++;
       cout <<"up"<<endl;
       break;
       case 3:
       y--;
       cout <<"down"<<endl;
      break;
       }
cout<<x<<","<<y<<endl;
//code below defines that when boundry coordinate is reached, put that value in the vector
if((x== 0 && y== 0)||(x== 0 && y== 1)||(x== 0 && y== 2 )||(x== 0 && y==   3)||(x== 0 && y== 4)||(y == 0 && x == 1)||(y == 0 && x == 2)||(y == 0 && x ==         3)||(y == 0 && x == 4)){
 values.push_back(grid[y][x]);
 numberOfValues++; //increase the number of values in the vector by 1
 x = 2; // reset x position
 y = 3;//reset y position
}
  cout <<numberOfValues<<endl; 

}





return 0;
}

Tl;DR 对于大量迭代变量,程序似乎跳过了这部分代码:(特别是 x=2,y=3 部分)

if((x== 0 && y== 0)||(x== 0 && y== 1)||(x== 0 && y== 2 )||(x== 0 && y==   3)||(x== 0 && y== 4)||(y == 0 && x == 1)||(y == 0 && x == 2)||(y == 0 && x ==         3)||(y == 0 && x == 4)){
 values.push_back(grid[y][x]);
 numberOfValues++; //increase the number of values in the vector by 1
 x = 2; // reset x position
 y = 3;//reset y position
}

注意:代码是使用 Visual Studio Ubuntu 上的代码编写的 注2:本人第一次在本站发帖,如有格式错误请告知,以免重蹈覆辙。

提前感谢您的帮助

I have the program print the "coordinates" of where it is on the grid and my intention was that it will never be a number that is not within the grid but that doesn't seem to be the case for when the iterations is a large.

这是因为您的边界测试并未涵盖所有情况。您似乎对每个点都进行了硬编码的单独测试,但经过所有这些努力,您只覆盖了两个边缘。

以下(更简单的)测试将告诉您是否处于任何边缘:

if (x == 0 || x == 4 || y == 0 || y == 4)

另请注意,如果您选择更改网格的尺寸,则调整边界测试会容易得多。事实上,更好的做法是将尺寸定义为常量而不是文字:

const int GRID_WIDTH = 5;
const int GRID_HEIGHT = 5;
int grid[GRID_HEIGHT][GRID_WIDTH];

// ...

    if (x == 0 || x == GRID_WIDTH - 1 || y == 0 || y == GRID_HEIGHT - 1)