【发布时间】:2014-04-07 19:58:25
【问题描述】:
我编写了一个代码来模拟 C++ 中的正态分布。但每次似乎结果都是一样的。我的问题是这种现象的原因是什么以及如何解决?我从来没有遇到过 Python 的这个问题。任何参考都非常感谢。
// Simulation.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include<random>
void main(){
// create default engine as source of randomness
// The maxtime we do expriements is 10000
// Record to the sum of the maxtimes sample
std::default_random_engine dre;
const int maxtimes = 10000;
double sum = 0.0 ;
// Generate the normal distribution witn mean 0 and variaiton 1.
std::normal_distribution<double> distribution(0.0, 1.0);
// Output the result and Record their sum.
for( int i=0; i<maxtimes; ++i)
{
double x = distribution(dre);
std::cout << x << ":";
sum +=x;
x =0.0;
}
std::cout<<std::endl;
std::cout <<" The average sum is: " << sum/10000 <<std::endl;
}
我的代码在 Visual C++ 2010 中运行。
【问题讨论】:
-
请注意格式。这就是预览面板的用途。
标签: c++ visual-c++ simulation random-sample