gpt4 book ai didi

c++ - C++ 中的 normrnd 函数

转载 作者:太空狗 更新时间:2023-10-29 21:46:20 32 4
gpt4 key购买 nike

在 MATLAB 中,函数 normrnd(mu,sigma) 用于生成正态随机数。它在 C++ 中的等效函数是什么,在哪个库下?

最佳答案

参见 random图书馆,这里是一个例子:

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>

int main()
{
std::random_device rd;

//
// Engines
//
std::mt19937 e2(rd());
//std::knuth_b e2(rd());
//std::default_random_engine e2(rd()) ;

//
// Distribtuions
//
std::normal_distribution<> dist(2, 2);
//std::student_t_distribution<> dist(5);
//std::poisson_distribution<> dist(2);
//std::extreme_value_distribution<> dist(0,2);

std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(dist(e2))];
}

for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}

这是normal_distribution的具体页面

关于c++ - C++ 中的 normrnd 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509081/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com