这是我更改代码之前的代码,以使其能够多次运行:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <random>
#include <iomanip>
#include <vector>
using namespace std;
int main () {
double vol;
double hit;
int samples;
int i, j;
double sum;
double pt;
double actual_vol;
const double PI = 2.0*atan2(1.0,0.0);
double abs_err;
double rel_err;
random_device dev;
default_random_engine e{ dev() };
uniform_real_distribution<double> u{0.0,1.0};
samples = 1000000 * dim;
actual_vol = pow(PI, double(dim/2.0)) / exp(lgamma(double(dim/2.0)+1.0));
for (i = 0; i < samples; i++) {
sum = 0;
for (j = 0; j < dim; j++) {
pt = 2*(u(e)-0.5);
sum += pt*pt;
}
if (sqrt(sum) < 1) {
hit += 1;
}
}
vol = ( pow(2,dim) * hit ) / samples;
abs_err = fabs( actual_vol - vol);
rel_err = abs_err / actual_vol;
cout << "Average volume of your sphere: " << setprecision(7) << vol << endl;
cout << "Actual volume: " << setprecision(7) << actual_vol << endl;
cout << "Absolute Error: " << setprecision(7) << abs_err << endl;
cout << "Relative Error: " << setprecision(7) << rel_err << endl;
}
我会得到正确的输出,看起来像这样:
Average volume of your sphere: 3.140924
Actual volume: 3.141593
Absolute Error: 0.0006686536
Relative Error: 0.000212839
现在,当我进行更改时,可以使用以下代码来调用该函数并多次运行:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <random>
#include <iomanip>
#include <vector>
using namespace std;
double monte_carlo (int dim) {
double vol;
double hit;
int samples;
int i, j;
double sum;
double pt;
double actual_vol;
const double PI = 2.0*atan2(1.0,0.0);
double abs_err;
double rel_err;
random_device dev;
default_random_engine e{ dev() };
uniform_real_distribution<double> u{0.0,1.0};
samples = 1000000 * dim;
actual_vol = pow(PI, double(dim/2.0)) / exp(lgamma(double(dim/2.0)+1.0));
for (i = 0; i < samples; i++) {
sum = 0;
for (j = 0; j < dim; j++) {
pt = 2*(u(e)-0.5);
sum += pt*pt;
}
if (sqrt(sum) < 1) {
hit += 1;
}
}
vol = ( pow(2,dim) * hit ) / samples;
abs_err = fabs( actual_vol - vol);
rel_err = abs_err / actual_vol;
cout << "Average volume of your sphere: " << setprecision(7) << vol << endl;
cout << "Actual volume: " << setprecision(7) << actual_vol << endl;
cout << "Absolute Error: " << setprecision(7) << abs_err << endl;
cout << "Relative Error: " << setprecision(7) << rel_err << endl;
}
int main (int argc, char* argv[]) {
int dim = 0;
int runs = 0;
int i;
dim = atoi(argv[1]);
runs = atoi(argv[2]);
for (i = 0; i < runs; i++) {
monte_carlo(dim);
}
return 0;
}
我得到这些结果,现在将以前的值与当前值相加,这不是我想要的:
Average volume of your sphere: 3.141764
Actual volume: 3.141593
Absolute Error: 0.0001713464
Relative Error: 5.454126e-05
Average volume of your sphere: 6.283674
Actual volume: 3.141593
Absolute Error: 3.142081
Relative Error: 1.000156
Average volume of your sphere: 9.427502
Actual volume: 3.141593
Absolute Error: 6.285909
Relative Error: 2.000867
Average volume of your sphere: 12.56937
Actual volume: 3.141593
Absolute Error: 9.427775
Relative Error: 3.000954
Average volume of your sphere: 15.71272
Actual volume: 3.141593
Absolute Error: 12.57113
Relative Error: 4.001515
Average volume of your sphere: 18.85378
Actual volume: 3.141593
Absolute Error: 15.71219
Relative Error: 5.001345
Average volume of your sphere: 21.99504
Actual volume: 3.141593
Absolute Error: 18.85345
Relative Error: 6.001239
您会注意到球体平均体积的第一个值是3.14,然后是第二个实例,现在是6.28(或第一个实例的两倍),第三个实例是9.42(大约是第一个实例的三倍)一个),等等。
它应该做的是在每次运行时运行一次新的计算,并且每个值都应该徘徊在3.14附近。如何停止上一次运行中的值求和?
谢谢!
答案 0 :(得分:4)
这可能是因为您从未重新初始化变量。
您还具有很强的“旧” C偏见(C标头,atoi
,fabs
...的使用),在需要它们时声明变量,并且路径始终在运行之所以类似,是因为您使用相同的随机数生成器和相同的种子(默认构造)。
仍然要解决您的问题:
double hit = 0;
double samples = 0;
以此类推。
对于PI,如果有增强功能,请使用其常数,而不要以比其所提供的精度低的精度重新计算它。