gpt4 book ai didi

c++ - 将 a==b 作为参数传递给 uniform_real_distribution ok

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:05 25 4
gpt4 key购买 nike

可以将 std::uniform_real_distributiona==b 一起使用吗?这种情况可以解释为没有随机性。

在 GNU c++ 库中:

explicit
param_type(_RealType __a = _RealType(0),
_RealType __b = _RealType(1))
: _M_a(__a), _M_b(__b)
{
_GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
}

断言接受 a==b 尽管它可能需要除以零。这是吗

  • 标准的扩展?
  • 一个无论如何都行得通的巧妙设计?
  • 碰巧在某些 CPU 上工作的错误?

最佳答案

相关规范段落[26.5.8.2.2, rand.dist.uni.real]/1说:

A uniform_real_distribution random number distribution produces random numbers x, ax < b, distributed according to the constant probability density function

p(x | a, b) = 1/(b − a) .

[Note: This implies that p(x | a, b) is undefined when a == b. — end note]

请注意,a <b 是对分布的约束。

有趣的是,构造函数(第 2 段)有一个更宽松的要求:

explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);

Requires: a ≤ b [...]

这意味着您可以使用a == b构造一个分布对象,但它描述的分布是未定义的。也就是说,您可能不会尝试使用这些参数(这可能确实会导致除以零或其他任何东西)为此类分布对象生成值。

另见 N3926征求意见为什么这两个约束不一致。该问题最初是作为 LWG 2168 提出的并已通过在第一段中添加引用的注释来解决。论文提请注意采用不同分布参数的operator()重载;结果是约束只在生成值时起作用,在构造分布对象时不起作用。

关于c++ - 将 a==b 作为参数传递给 uniform_real_distribution ok,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com