一个函数中的局部变量更改会导致不相关的函数 C++
预先警告大家,我是一名物理学家,正在编写一个模拟,从专业程序员的角度来看,这可能是玩具代码,所以我的编码知识不是很好。不管怎样,解释一下 700 行,我的代码看起来像下面这样:
#define _USE_MATH_DEFINES
#include "MersenneTwister.h" //Random number generator
#include "Particle.h" //Class that just keeps track of coordinates
#include <algorithm>
#include <vector>
using namespace std;
const double SomeGlobalParameters;
//Filling an array with gaussian random numbers
void GaussianDisorder(double EnergyArray[Nx][Ny][Nz], double Sigma)
{
for (int i=0; i<Nx; i++){
for (int j=0; j<Ny; j++){
for (int k=0; k<Nz; k++){
EnergyArray[i][j][k] = rnd.randNorm(0, Sigma);
}
}
}
}
//Using the above array (read only) to do some calculations
void CalcRates(Particle &charge1, const double (&EnergyArray)[Nx][Ny][Nz], double (&RateArray)[12])
{
int X1 = charge1.xPosition();
double NearRate, MagSqr, Dist, OldDist, OldEnergy, NewEnergy; //Declaring stuff for later
const double Parity=1.0;
InitializeStuffAbove(SomeGlobalParameters); // Give stuff values based on parameters
if (Dist == 0)
RateArray[0] = NearRate;
else
NewEnergy = Parity*EnergyArray2[X1+1][Y1][Z1] - Efield[0] + Coulomb/(Dist);
int main()
{
double EnergyArray[Nx][Ny][Nz];
double RateArray[12];
GaussianDisorder(EnergyArray);
CalcRates(charge1, EnergyArray, RateArray);
return 0;
}
我的问题是,当我将
const double Parity=1.0;
,这是一个局部变量,更改为
const double Parity=-1.0;
我的随机数生成器时,
void GaussianDisorder
给出了不同的结果,即使我给了它一个常量种子,并且它给出了如果我多次运行代码而不更改任何内容,则顺序相同。如果我遗漏了一些重要的内容,我提前道歉。
Just to forewarn folks, I'm a physicist writing a simulation which is probably toy code from a professional programmer's perspective, so my coding knowledge isn't great. Anyway, paraphrasing 700 lines, my code looks something like the following:
#define _USE_MATH_DEFINES
#include "MersenneTwister.h" //Random number generator
#include "Particle.h" //Class that just keeps track of coordinates
#include <algorithm>
#include <vector>
using namespace std;
const double SomeGlobalParameters;
//Filling an array with gaussian random numbers
void GaussianDisorder(double EnergyArray[Nx][Ny][Nz], double Sigma)
{
for (int i=0; i<Nx; i++){
for (int j=0; j<Ny; j++){
for (int k=0; k<Nz; k++){
EnergyArray[i][j][k] = rnd.randNorm(0, Sigma);
}
}
}
}
//Using the above array (read only) to do some calculations
void CalcRates(Particle &charge1, const double (&EnergyArray)[Nx][Ny][Nz], double (&RateArray)[12])
{
int X1 = charge1.xPosition();
double NearRate, MagSqr, Dist, OldDist, OldEnergy, NewEnergy; //Declaring stuff for later
const double Parity=1.0;
InitializeStuffAbove(SomeGlobalParameters); // Give stuff values based on parameters
if (Dist == 0)
RateArray[0] = NearRate;
else
NewEnergy = Parity*EnergyArray2[X1+1][Y1][Z1] - Efield[0] + Coulomb/(Dist);
int main()
{
double EnergyArray[Nx][Ny][Nz];
double RateArray[12];
GaussianDisorder(EnergyArray);
CalcRates(charge1, EnergyArray, RateArray);
return 0;
}
My problem is when I change
const double Parity=1.0;
,which is a local variable, to
const double Parity=-1.0;
my random number generator in
void GaussianDisorder
gives a different result, even though I gave it a constant seed, and it gives the same sequence if I run the code multiple times without changing anything. I apologize in advance if I left something important out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜 rnd 是在 MersenneTwister.h 中声明的,如果是的话,函数 randNorm(const double Mean, const double stddev) (从这里 http://www-personal.umich.edu/~wagnerr/MersenneTwister.h)使用函数 rand()。
为了每次都有不同的值,您应该联合使用 rand 和 srand。看这里: http://www.cplusplus.com/reference/clibrary/cstdlib/rand /。
I guess rnd is declared in MersenneTwister.h, if so function randNorm(const double mean, const double stddev) (from here http://www-personal.umich.edu/~wagnerr/MersenneTwister.h) using function rand().
In order to have different values every time you should use rand and srand jointly. Look here: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/.