在C++11以上中在# include <random> 头文件中包含了std::normal_distribution<>b;正态分布函数。
当然也可以下载boost库,里面已经有包装好的正态分布函数。
1.boost库可以在https://www.boost.org/中进行下载最新版的
2.boost库导入C++工程的方法可以参照博客https://blog.youkuaiyun.com/loongsking/article/details/80136004
3.导入好了boost库后示例代码如下
#include <boost\math\distributions\normal.hpp>
#include <iostream>
#include <random>
#include <math.h>
int main() {
//std::normal_distribution<>b;
boost::math::normal_distribution<> norm(0, 1);//期望,方差
double PI = acos(-1);
std::cout<<boost::math::cdf(norm,0)<<std::endl; //0.5 概率密度值,小于0的概率
std::cout << boost::math::pdf(norm, 0) - 1 / (sqrt(2 * PI)) << std::endl; //0 正态分布函数值
std::cout << boost::math::cdf(boost::math::complement(norm,1)) << std::en

本文介绍在C++中如何使用std::normal_distribution及boost库实现正态分布函数,包括期望、方差的设置,概率密度和正态分布函数值的计算。同时,提供了手动实现标准正态分布概率累积函数的方法。
最低0.47元/天 解锁文章
361

被折叠的 条评论
为什么被折叠?



