c ++ 11随机引擎和发行版的交互和成本 [英] c++11 random engines and distributions interaction and cost

查看:45
本文介绍了c ++ 11随机引擎和发行版的交互和成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对随机引擎和发行版,它们的成本和交互性有很多疑问:

I have a bunch of questions about random engines and distributions, their cost and interaction:

  1. std :: random_device 是一个昂贵的(可大量构建的)对象吗?如果可以的话,我应该只为我的应用程序创建一个吗?
  2. RandomNumberDistribution 对象是否是昂贵的对象,还是主要取决于具体分布?在我看来,例如 uniform_int_distribution 应该是非常轻的对象,包含分布范围 [min,max] ,并且可能是(?)一些内部状态.
  3. random_device random_engine distribution 之间的交互如何在以下代码中起作用:

  1. Is std::random_device an expensive (heavy constructible) object? Should I only create one for my application if possible?
  2. Are RandomNumberDistribution objects expensive objects or is it mostly dependent of concrete distribution? It seems to me that for example uniform_int_distribution should be very light object containing distribution range [min, max] and may be (?) some internal state.
  3. How does the interaction between random_device, random_engine and distribution work in code below:

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 10);

auto random_value = distr(gen);

最后一行会发生什么?分布从随机引擎获取下一个值,然后将其带到所需范围 [0,10] ?显然,某些对象的内部状态总是在调用后进行修改:肯定是 random_device random_engine ,但是 uniform_int_distribution 是否也具有某些内部状态?

What happens in last line? Distribution get next value from random engine and then brings it to desired range [0, 10]? Always obviously internal states of some objects should be modified after call: definitely random_device, random_engine but does uniform_int_distribution also have some internal state?

推荐答案

1)通常不是这样,初始化对于某些PRNG初始化来说是微不足道的或可映射的,但是调用 operator()可能会访问用于获得安全随机数的OS,这可能会花费很大.

1) Usually it isn't, initialization either is trivial or comaprable to some PRNG initialization, however invoking operator() might access OS for secure random number and it could be costly.

2)主要取决于分布.它们包含分布参数(如果需要,您可以检索)和可选地某些内部状态(IIRC大多数实现都不会干扰它).因此,初始化 distribution 就像初始化小型结构一样.

2) Mostly depends on distribution. They containg distribution parameters (which you can retrieve if you want) and optionally some inner state (IIRC most implementation doesn't bother with it). So initializating distribution is like initializating small structure.

3) Distribution 对象调用未指定次数(从0到∞次)的 generators operator()并从中创建一个随机数 generator 产生的位序列.然后返回生成的bumber.它可能会提升 generator 状态,并可能将 generator 产生的未使用位存储在 distribution 内部状态中.

3) Distribution object invokes generators operator() unspecified amount of times (from 0 to ∞ times) and creates a random number from bit sequence produced by generator. Then it returns generated bumber. It potentially advances generator state and might store unused bits produced by generator in distribution internal state.

random_device 实现为系统RNG的句柄,它甚至可能没有内部状态.否则,当您调用它的 operator()生成随机数时,它的状态会在第二行前进.您将使用它作为 generator 的种子.

random_device, probably, does not even has internal state, if it implemented as handle to system RNG. Otherwise, its state is advanced on second line, when you invoke its operator() to produce random number you will use as seed for your generator.

这篇关于c ++ 11随机引擎和发行版的交互和成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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