我是一个编码初学者,编写了我的第一个更大的C++项目(飞机初始尺寸程序)。该代码应该能够以概率的方式分析设计,使用蒙特卡罗模拟。因此,我必须为几个独立的随机变量生成随机值。
当我想学习如何编写好的代码时,我要感谢您回顾了简化的‘随机数生成’--这是我代码的一部分:
settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
#include <iostream>
#include <string>
#include <vector>
class settings
{
public:
struct distInput
{
std::string distName;
double paramA;
double paramB;
double paramC;
};
std::vector<distInput> distInputs;
settings();
};settings.cpp
#include "settings.h"
settings::settings()
{
//Read input from file, here simplified
distInput input1 =
{
"Normal Distribution",
100.,
1.,
0.
};
distInput input2 =
{
"Triangular Distribution",
80.,
100.,
109.
};
distInputs.push_back(input1);
distInputs.push_back(input2);
}
//here are more functionsuncertainty.h
#ifndef UNCERTAINTY_H
#define UNCERTAINTY_H
#include <iostream>
#include <string>
#include <random>
#include <vector>
class uncertainty
{
public:
void generateRN();
std::vector<std::mt19937_64> randomNumberGenerators;
std::vector<std::vector<double>> randomNumbers;
settings *mySettingsPt;
std::vector<settings::distInput> *inputData;
uncertainty(settings &mySettings, double numbersRV);
};uncertainty.cpp
#include "uncertainty.h
uncertainty::uncertainty(settings &mySettings, double numbersRV)
:
mySettingsPt(&mySettings),
inputData(&mySettingsPt->distInputs)
{
for(unsigned int i = 0; i < numbersRV; ++i)
{
std::random_device rd;
std::mt19937_64 mt_tmp(rd());
randomNumberGenerators.push_back(mt_tmp);
randomNumbers.resize(numbersRV);
}
}
void uncertainty::generateRN()
{
unsigned int numRV = inputData->size();
for(unsigned int i = 0; i < numRV; ++i)
{
std::string name = inputData->at(i).distName;
double a = inputData->at(i).paramA;
double b = inputData->at(i).paramB;
double c = inputData->at(i).paramC;
if(name == "Normal Distribution")
{
std::normal_distribution<> dist(a, b);
randomNumbers.at(i).push_back(dist(randomNumberGenerators.at(i)));
}
else if(name == "Triangular Distribution")
{
std::uniform_real_distribution<> dist(0, 1);
double F = (b - a) / (c - a);
double U = dist(randomNumberGenerators.at(i));
if (U <= F)
randomNumbers.at(i).push_back(a + sqrt(U * (c - a) * (b - a)));
else
randomNumbers.at(i).push_back(c - sqrt((1 - U) * (c - a) * (c - b)));
}
}
};main.cpp
#include "settings.h"
#include "uncertainty.h"
#include <iostream>
#include <string>
#include <vector>
#include <random>
int main()
{
// Initialize objects for computation
settings mySettings;
uncertainty myUncertainty(mySettings, mySettings.distInputs.size());
// Start Computation
for(unsigned int i = 0; i < 100; ++i)
{
myUncertainty.generateRN();
// Other code, that uses the random values
}
}代码工作正常,但我从来没有让人检查过我的代码技能。任何类型的改进建议(如何构造.cpp和h.文件,如何更有效地编写代码,如何使用数据结构和对象等)非常欢迎!
发布于 2016-09-24 01:08:26
显然,您已经花了一些时间研究最佳实践;您的代码是分开的、可读的、逻辑的。保持格式化,人们将乐于阅读您的代码。
不过,这确实让我大吃一惊:您有两个特定的字符串文本“正态分布”和“三角分布”,它们在单独的文件中重复。建议以后避免字符串文字的多个实例,特别是在单独的文件中;当代码变大时,这样的方法可能会耗时且错误。
想想看:常数和枚举。您的编译器不会发现这样的字符串文字类型,这可能会导致令人恼火的运行时错误或意外行为。另一方面,它当然会提醒您注意代码中未声明的(错误类型)句柄。更有效的清除bug。
https://codereview.stackexchange.com/questions/142262
复制相似问题