c++在循环中定义一组概率分布

标签 c++ loops random

我想用不同的方式创建一组正态分布。 像这样:

#include <random>
using namespace std;
std::default_random_engine generator;
std::normal_distribution<double> normal_1(0.0,std_1);
std::normal_distribution<double> normal_2(0.0,std_2);
// and so on until:
std::normal_distribution<double> normal_N(0.0,std_N);

我需要能够更改分布的总数,以便循环更好地工作,如下所示:

for (int i = 1; i<=N; i++){
std::normal_distribution<double> normal_i(0.0,std_i);
}

PD:我是 C++ 的新手,所以这可能是一个非常基本的问题。

最佳答案

正如评论中反常指出的那样,std::vector做这份工作。

#include <random>
#include <iostream>
#include <vector>
using namespace std;
default_random_engine generator;


int main()
{
    vector<normal_distribution<double>> v = {};

    for (int i=1; i<=N; i++)
    {
        v.push_back(normal_distribution<double> (0.0,std_i));
    }
    //v[i-1](generator); generate a random number from the distribution i
}

关于c++在循环中定义一组概率分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564979/

相关文章:

javascript - 如何获取 HTML 表单中的 JS 函数结果?

c++ - boost::this_thread::sleep() 与 nanosleep()?

c++ - 这个私有(private)变量 "not declared in this scope"怎么样?

c++ - 正确支持 STL 中成员函数的 volatile 限定符

c++ - 没有 "new"的多态性

Javascript - 试图在动态属性中包含对象

linux - 在循环中使用时 awk 出现索引错误

java - 按 <logic :iterate> 中的插入顺序迭代 HashMap

c++ - 获取序列C++中的随机数

swift - 为 SWIFT 序列下的 repeatActionforever 内的子 Action 生成随机时间