- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我认为 c++11 随机分布(例如 uniform_int_distribution
)生成的值仅取决于传递给 operator() 的生成器的状态
。但是,由于某种原因,operator()
的签名中没有 const
说明符。这是什么意思,我应该如何将分布作为函数参数传递?我以为我必须将它作为任何非可变参数传递:通过 const 引用,但现在我不确定。
最佳答案
一开始我误解了这个问题,但是现在我明白了,这是一个很好的问题。一些深挖<random>
的实现源码for g++ 给出了以下内容(为清楚起见省略了一些位):
template<typename _IntType = int>
class uniform_int_distribution
{
struct param_type
{
typedef uniform_int_distribution<_IntType> distribution_type;
explicit
param_type(_IntType __a = 0,
_IntType __b = std::numeric_limits<_IntType>::max())
: _M_a(__a), _M_b(__b)
{
_GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
}
private:
_IntType _M_a;
_IntType _M_b;
};
public:
/**
* @brief Constructs a uniform distribution object.
*/
explicit
uniform_int_distribution(_IntType __a = 0,
_IntType __b = std::numeric_limits<_IntType>::max())
: _M_param(__a, __b)
{ }
explicit
uniform_int_distribution(const param_type& __p)
: _M_param(__p)
{ }
template<typename _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng)
{ return this->operator()(__urng, this->param()); }
template<typename _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __p);
param_type _M_param;
};
如果我们斜视所有 _
,我们可以看到它只有一个成员参数,param_type _M_param
,它本身只是一个嵌套结构,包含 2 个整数值 - 实际上是一个范围。 operator()
只在这里声明,没有定义。更多的挖掘将我们带到了定义上。代替在这里发布所有代码,这非常丑陋(而且相当长),只要说这个函数内部没有任何变化就足够了。实际上,添加 const
to 定义和声明将愉快地编译。
那么问题就变成了,对于其他所有发行版都是这样吗?答案是不。如果我们看一下 std::normal_distribution
的实现,我们发现:
template<typename _RealType>
template<typename _UniformRandomNumberGenerator>
typename normal_distribution<_RealType>::result_type
normal_distribution<_RealType>::
operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __param)
{
result_type __ret;
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng);
//Mutation!
if (_M_saved_available)
{
_M_saved_available = false;
__ret = _M_saved;
}
//Mutation!
这只是理论上的,但我想它不限于 const
的原因是允许实现者在需要时改变他们的实现。此外,它保持了更统一的界面 - 如果一些 operator()
是 const
有些不是- const
,一切都变得有些困惑。
但是,为什么他们不简单地将它们设为 const 并让实现者使用 mutable
我不确定。很可能,除非附近有人参与了这部分标准化工作,否则您可能无法得到很好的答案。
编辑:正如 MattieuM 指出的,mutable
并且多个线程不能很好地协同工作。
顺便说一句,std::normal_distribution
一次生成两个值,缓存一个(因此是 _M_saved
)。 operator<<
它定义的实际让你在下次调用 operator()
之前看到这个值。 :
#include <random>
#include <iostream>
#include <chrono>
std::default_random_engine eng(std::chrono::system_clock::now().time_since_epoch().count());
std::normal_distribution<> d(0, 1);
int main()
{
auto k = d(eng);
std::cout << k << "\n";
std::cout << d << "\n";
std::cout << d(eng) << "\n";
}
这里,输出格式是mu sigma nextval
.
关于c++ - 为什么 c++11 随机分布是可变的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008271/
我有两个项目。一个项目正在运行,没有任何问题。它是从 gitlab 下载的。另一个项目是从 github 下载的。 github项目有这个问题。我想使用默认的 gradle 分布。我想知道我做错了什么
我正在通过我学习的大学提供的 VNC 软件(远程访问)使用 IBM bigInsights,但我无法通过该桌面访问 Internet。为了使用互联网上的一些数据样本,我决定安装 Hadoop 在我的笔
所以,这非常简单,我有一个包含嵌套列表的列表,如下所示: List( *list1* List(List("n1", "n3"), List("n1", "n4"), List("n3", "n4")
我有以下示例。 prefix = ['blue ','brown '] suffix = [('dog','shoes','bike'), ('tree','cat','car')] 我想获得一个如下
我创建了一项调查并将其发送出去。该调查要求用户提供电子邮件,然后要求他们从包含 8 个不同选项的下拉菜单中选择要吃哪顿饭。有些人使用同一封电子邮件多次填写调查,但食物选择不同。 我有一个如下所示的 M
我在 Python 中使用 plotly 来创建由某些分类变量着色的美国县的等值线。由于县非常小,因此图像中的边界线占主导地位。我怎样才能摆脱它们(或将它们的宽度设置为零)? 到目前为止的代码和输出(
我们有qgamma在 R 和 gamm.inv在 excel 中,我无法使用 invgamma 获得相同的结果python中的函数。例如在excel中GAMMA.INV(0.99,35,0.08)=4
过去几年我经常使用 Docker,但对于 Kubernetes 来说我还是个新手。我从今天开始,与我以前使用 Docker swarm 的方式相比,我正在努力思考 Pod 概念的实用性。 假设我有一个
我有一个 UIStackView然而,subViews的第一个 View 是 UILabel它没有相应地调整它的大小。 我的代码如下; private let stackView: UIStackVi
我想绘制自由度为 1、2、5 和 10 的 Student t 分布;所有在一个图中,并为图中的每个分布使用不同的颜色。此外,在 Canvas 的左上角创建一个图例,并增加 df = 1 的曲线线宽。
我对 Python 很陌生,我在互联网上浏览过,但找不到任何可以帮助我解决问题的逻辑。 我在图中有降水值,现在我需要根据图中的这些值拟合 GEV 分布。每个值等于从 1974 年到 2017 年的一年
我正在尝试复制此图 https://wind-data.ch/tools/weibull.php 我编写的代码是 import matplotlib.pyplot as plt import nump
对于家庭作业,我必须绘制文本的词频并将其与最佳 zipf 分布进行比较。 根据对数对数图中的排名绘制文本的词频计数似乎效果很好。 但是我在计算最佳 zipf 分布时遇到了麻烦。结果应该如下所示: 我不
Mathematica 具有四参数广义逆 Gamma 分布: http://reference.wolfram.com/mathematica/ref/InverseGammaDistribution
正在用 C 语言开发一个学校项目,使用 Pthreads 将一维数组分解为 tRows 和 tCols 的子矩阵。整个数组的大小为 wRows 和 wCols。假设 wCols = 4、wRows =
有没有办法得到制服int32_t没有警告的分发?我用这个uniform_int_distribution在我的代码中,但我收到警告: 54988961.cpp: In function ‘int ma
在花了相当多的时间试图了解如何在 postgresql 数据库服务器之间实现负载平衡(分配数据库处理负载)之后,我来到这里。 我有一个 postgresql 系统,每秒吸引大约 100 笔交易,而且这
所以标题已经说明了一切。我们正在开发一个开始获得大量依赖项的项目。到目前为止,我们一直在使用 setuptools,但越来越多的依赖项要么不容易安装(例如 wxPython),要么在某些使用 easy
我有以下代码: #include #include #include using namespace boost::numeric; using namespace interval_lib;
我有一个对象列表,我想以随机顺序连续访问这些对象。 我想知道是否有一种方法可以确保随机值并不总是相似。 例子。 我的列表是队列列表,我试图交错这些值以生成用于测试的真实场景。 我并不是特别想要队列 1
我是一名优秀的程序员,十分优秀!