gpt4 book ai didi

C++ 数值库:std::uniform_int_distribution<>,更改调用之间的分布范围

转载 作者:行者123 更新时间:2023-11-30 01:50:51 31 4
gpt4 key购买 nike

我有类似下面的代码:

    vector<int> vec;
// stuff vector here

random_device rd;
minstd_rand generator(rd());
uniform_int_distribution<unsigned> dist(0 , vec.size() - 1);
while (vec.size() > 0)
{
auto it = vec.begin() + dist(generator);
// use *it for something
swap(*it, *(vec.end() - 1));
vec.pop_back();
}

我知道我可以在循环内构造/破坏局部分布。但我宁愿只调整循环内 dist 的边界。我可以这样做吗?

最佳答案

param呢? ?

dist.param( decltype(dist)::param_type(otherMin, otherMax) );

C++11 标准(及后续标准),[rand.req.dist]/9:

For each of the constructors of D taking arguments corresponding to parameters of the distribution, P shall have a corresponding constructor subject to the same requirements and taking arguments identical in number, type, and default values.

关于C++ 数值库:std::uniform_int_distribution<>,更改调用之间的分布范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947324/

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