C++ C+中的高斯随机正态分布+;

C++ C+中的高斯随机正态分布+;,c++,statistics,C++,Statistics,我用Python编写了正态分布函数。我需要把它转换成C++,我不熟悉语言。 这是我的Python: def calculation(value): sigma = 0.5 size = 10000 x = 200 x_distribution = np.random.normal(value, sigma, size) for i in x_distribution: x.append(i) return x 它的工作原理与预期

我用Python编写了正态分布函数。我需要把它转换成C++,我不熟悉语言。 这是我的Python:

def calculation(value):
    sigma = 0.5
    size = 10000
    x = 200

    x_distribution = np.random.normal(value, sigma, size)
    for i in x_distribution:
        x.append(i)
    return x
它的工作原理与预期一致。我试图在C++中重写同样的东西,只找到“ST::常态分布D{5,2}”的位置和位置; “我必须变魔术。但我不知道如何实施

这里是我尝试过的,但失败了

# include frame.distribution
Frame DistributionModel(x_mu, x_sigma)
{
    // Motion model;ignore it
    model = std::normal_distribution<> d{x_mu,x_sigma};

    return model;
}
#包括frame.distribution
帧分布模型(x_μ,x_σ)
{
//运动模型;忽略它
model=std::正态分布d{x_mu,x_sigma};
收益模型;
}

请帮帮我。寻找任何线索。谢谢

好吧,麻烦不断

# include frame.distribution
包含的语法为:

#include <name_of_header_file>
// or:
#include "name_of_header_file"
C++是一种强类型语言,即。E您不能像Python中那样只给变量一个名称,但需要给它们一个类型

Frame DistributionModel(double x_mu, double x_sigma)
局部变量也一样;类型必须与实际分配的内容匹配(除非使用自动)

关于返回值:如果在某处定义了数据类型,则只能返回
Frame
。现在,在尝试定义一个新类之前,我们可以使用一个现有的类:
std::vector
(不过它是一个模板类)。向量非常类似于python列表,它是在连续内存中存储大量对象的容器类;但是,除了python列表之外,存储的所有元素的类型必须相同。我们可以使用这样的向量来收集结果:

std::vector<double> result;
向量是我们要返回的,因此我们需要调整函数签名(我允许给它一个不同的名称并添加另一个参数):


<代码> ND(Gen)< /C> >:代码> STD::NojiGrimeDistabor <代码>提供函数调用运算符<代码>操作程序()/>代码>,因此对象的调用可以像函数一样调用(此类对象在C++术语中被称为“函子”)。然而,函数调用需要一个随机数生成器作为参数,因此我们需要提供它,正如您看到的示例中所示。综合起来:

#include <random>
#include <vector>

std::vector<double> getDistribution
(
    double x_mu, double x_sigma, size_t numberOfValues
)
{
    // shortened compared to your example:
    std::mt19937 gen((std::random_device())());
    // create temporary (anonymous)     ^^
    // instance and call it immediately    ^^
    // afterwards

    std::normal_distribution<double> nd(x_mu, x_sigma);
    std::vector<double> result;
    result.reserve(numberOfValues);
    while(numberOfValues-- > 0)
    {
        // shorter than above: using result of previous
        // function (functor!) call directly as argument to next one
        result.push_back(nd(gen));
    }

    // finally something familiar from python:
    return result;
}
#包括
#包括
向量分布
(
双x_μ,双x_σ,大小\u t数值
)
{
//与您的示例相比缩短了:
std::mt19937 gen((std::random_device())());
//创建临时(匿名)^^
//实例并立即调用它^^
//后来
正态分布nd(x_μ,x_σ);
std::向量结果;
结果.保留(数值);
while(numberOfValues-->0)
{
//短于上述内容:使用上一页的结果
//函数(functor!)直接作为参数调用下一个函数
结果:推回(nd(gen));
}
//最后是python中熟悉的内容:
返回结果;
}

std::正态分布d{x_μ,x_σ}
将s设为变量
d
,但不处理它。看起来您可能想使用
d
生成
大小
数字。一个
for
循环和一个
std::vector
可能对您有用。不知道什么是
框架
模型
,所以我只能帮你了。但是你熟悉python,是吗?您应该注意正确的格式,尤其是在缩进具有重要意义的语言中,如在python中…假设您应该先了解一个好的格式…是的,我对python很熟悉。我从我的环境中复制和粘贴,StackO刚刚修改了我的函数,即使我试图修复它,我建议您在担心生成随机数之前,先通过一个测试,确保掌握了基本知识。不幸的是,C++和Python有着非常不同的基本语法。哇,那么有用和明确的答案。非常感谢你!
 std::vector<int> v;
 std::vector<int> v(10); // vector with 10 elements.
#include <random>
std::vector<double> result;
result.reserve(max);
std::vector<double> getDistribution(double x_mu, double x_sigma, size_t numberOfValues)
while(numberOfValues-- > 0)
{
    auto value = nd(gen);
    result.push_back(value);
}
#include <random>
#include <vector>

std::vector<double> getDistribution
(
    double x_mu, double x_sigma, size_t numberOfValues
)
{
    // shortened compared to your example:
    std::mt19937 gen((std::random_device())());
    // create temporary (anonymous)     ^^
    // instance and call it immediately    ^^
    // afterwards

    std::normal_distribution<double> nd(x_mu, x_sigma);
    std::vector<double> result;
    result.reserve(numberOfValues);
    while(numberOfValues-- > 0)
    {
        // shorter than above: using result of previous
        // function (functor!) call directly as argument to next one
        result.push_back(nd(gen));
    }

    // finally something familiar from python:
    return result;
}