视觉 正态分布的每个模拟都是相同的 (C++)
visual Each simulation of normal distribution is the same (C++)
我编写了一个代码来模拟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 中运行。
您每次都从同一种子构造default_random_engine
: 由于您没有给它一个种子来构造,它只是使用默认值,每次运行都相同,因此每次运行都会得到相同的"随机"数字。 http://www.cplusplus.com/reference/random/linear_congruential_engine/linear_congruential_engine/
使用random_device为生成器设定种子。
std::default_random_engine dre(std::random_device()());
尝试:
std::random_device mch;
std::default_random_engine generator(mch());
std::normal_distribution<double> distribution(0.0, 1.0);
相关文章:
- 改变或缩放两个正态分布以具有特定的相关系数
- 从 C++ 函数与 Python 函数返回的不一致值用于偏斜正态分布
- 逆生存函数与正态分布的PPF(ICDF)?
- C 中高斯的随机正态分布
- C :实用程序名称空间中的正态分布
- MT19937和正态分布
- 生成C++中的正态分布随机数
- 如何从限于一定间隔的正态分布进行采样,即C 实现
- 带参数的累积正态分布函数的逆
- 在.h文件中声明正态分布生成器
- Box Muller Transform在实现正态分布PRNG时的问题
- 接受拒绝算法 C++ 正态分布
- 在C 中实现正态分布的统一比率
- 如何创建一个在单独的方法中工作的c++随机正态分布
- 如何在C++中随机采样具有给定平均值和标准误差的正态分布
- C++中对数正态分布的累积函数的逆
- C++生成范围内服从正态分布的随机数
- 在 gmp 任意精度上生成正态分布
- 如何计算 CDF C++中正态分布概率
- 随机数的正态分布会挂起程序