首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stl: exponential_distribution -使用最小值和最大值作为输入参数

Stl: exponential_distribution -使用最小值和最大值作为输入参数
EN

Stack Overflow用户
提问于 2019-09-06 20:14:31
回答 1查看 44关注 0票数 0

您能告诉我如何为exponential_distribution分布创建一个对象吗?在这个分布中,我只指定可以作为输入参数的最小值和最大值。

代码语言:javascript
复制
m_generatorDeltaTime = std::exponential_distribution<double>(minDelta, maxDelta);

上面的示例给出了一个错误:(

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-06 20:23:54

exponential distribution只有一个参数,这个参数决定了它的形状。

最小值始终为0,没有最大值。

您可以编写自己的分发对象,并使用std::exponential_distribution作为其中的一部分。

代码语言:javascript
复制
template <typename RealType>
struct my_distribution
{
    struct param_type : typename std::exponential_distribution<RealType>::param_type
    {
        RealType min;
        RealType max;
    };

    using result_type = RealType

    my_distribution(RealType lambda, RealType min, RealType max) : m_exp(lambda), m_param(lambda, min, max) {}
    my_distribution(param_type param) : m_exp(param), m_param(param) {}

    template <typename URBG>
    result_type operator()(URBG & gen)
    {
        result_type initial = m_exp(gen);
        // some calculation of value here involving m_param.min, m_param.max
        return value;
    }

    template <typename URBG>
    result_type operator()(URBG & gen, const param_type & param)
    {
        result_type initial = m_exp(gen, param);
        // some calculation of value here involving param.min, param.max
        return value;
    }

    param_type param() { return m_param; }
    void param(const param_type & param) { m_exp.param(param); m_param = param; }

    result_type lambda() { return m_exp.lambda(); }
    result_type min() { return m_param.min; }
    result_type max() { return m_param.max; }
private:
    std::exponential_distribution<RealType> m_exp;
    param_type m_param;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57821852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档