如何在标准构造函数之外设置boost :: uniform_int_distribution的参数? [英] How to set the parameters of boost::uniform_int_distribution outside of the standard constructor?

查看:203
本文介绍了如何在标准构造函数之外设置boost :: uniform_int_distribution的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost 1.52中的uniform_int_distribution来使用基本样板代码生成随机数:

I am using a uniform_int_distribution in Boost 1.52 to generate random numbers using the basic boilerplate code:

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

boost::random::mt19937 gen;

int roll_die()
{
    boost::random::uniform_int_distribution<> dist(1, 6);
    return dist(gen);
}

int main()
{
    for (int i = 0; i < 10; i++) std::cout << roll_die() << std::endl;     
}

我在一个更大的项目中实现了其中的一部分,并且效果很好.这是我的问题.

I implemented parts of this in a much larger project and it works great. Here is my question.

在上面的函数中,似乎dist对象是该函数的本地对象.如果您多次调用roll_die(),似乎dist()在该函数中是本地的,则会带来很多开销.

In the above function, it seems like the dist object is local to the function. If you call roll_die() many many times, it seems like having dist() be local to the function would introduce a lot of overhead.

我认为最好一次设置此对象的min和max参数,然后在更大的对象或某物中仅包含dist的一个实例.如何做到这一点?我试图理解类模板的公共成员函数"部分:

I'm thinking it would be better to set the min and max parameters of this object once, and then only have one instance of dist in a bigger object or something. How does one do this? I tried to understand the "Public Member Functions" portion of the class template: http://www.boost.org/doc/libs/1_47_0/doc/html/boost/random/uniform_int_distribution.html#id744736-bb but it was over my head. In that documentation I see:

void param(const param_type & param); //Sets the parameters of the distribution.

您实际上如何使用它? .param()本身是要调用的函数,还是其他函数的替代者?我找不到另一个能满足我要求的增强示例.预先感谢您的协助和建议!

How do you actually use this? Is .param() itself a function to call, or is it a stand-in for another function? I couldn't find another boost example that did what I'm asking. Thanks in advance for your assistance and advice!

推荐答案

似乎……会带来很多开销.

it seems like ... would introduce a lot of overhead.

您可能会认为确实如此,但是您实际上知道吗?如果没有实际运行分析器或以其他某种方式对代码进行基准测试,则不应做出任何假设,以决定是更快还是更慢.如果您查看源代码,您会发现uniform_int_distribution的构造函数仅分配最小值和最大值-这实际上是微不足道的开销.特别是如果您考虑到实际的随机数生成将比两个分配复杂得多.因此,我建议您按原样保留代码,如果程序太慢,则可以随时进行概要分析然后进行优化.

You may assume it does, but do you actually know this? You should not make any assumptions if anything is faster or slower without actually running a profiler or benchmarking the code in some other way. If you look at the source, you can see that uniform_int_distribution's constructor merely assigns the min and max values - which should be really insignificant overhead. Especially if you take into account that the actual random number generation will be a much more complex operation than two assignments. So I'd suggest you leave the code as is, and if your program is too slow, you can always profile and then optimize.

引用Djikstra的话:过早的优化是万恶之源".通常,程序员编写比所需要复杂的代码,仅仅是因为他们认为这样做会更快.不要这样做-仅在出现速度问题时才开始优化.

To quote Djikstra: "Premature optimization is the root of all evil". Way too often, programmer's write more complex code than need be, simply because they think it will be faster. Don't do it - only start optimizing when there are speed problems.

无论如何,要回答您的问题:param()uniform_int_distribution的成员.它采用类型为uniform_int_distribution::param_type的对象.您可以像这样使用它:

Anyway, to answer your questions: param() is a member of uniform_int_distribution. It takes an object of type uniform_int_distribution::param_type. You can use it like so:

using namespace boost::random;

// Create an uniform_int_distribution object
uniform_int_distribution<> dist(1, 6);

// Create a params object
uniform_int_distribution::param_type newParams(10, 500);

// The following will reconfigure dist to have 10 and 500 as 
// min and max value
dist.param(newParams);

这样,您可以根据需要多次重新配置单个分发对象.但是开销"可能与构造新的分发对象相同.

This way, you can reconfigure a single distribution object as often as you like. But the "overhead" will likely be the same as constructing a new distribution object.

另一种可以确保仅创建对象一次的方法:

Another way you can ensure that the object is only created once:

int roll_die()
{
    static boost::random::uniform_int_distribution<> dist(1, 6);
    return dist(gen);
}

将函数内部的变量声明为静态具有与全局变量相似的效果,但仅在函数范围内可见.

Declaring variables inside of functions as static has a similar effect as if the variable where global, but it's only visible in the function's scope.

这篇关于如何在标准构造函数之外设置boost :: uniform_int_distribution的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆