如何解决std :: shuffle的顺序是否可以依赖RNG以外的任何内容?
要以相同顺序对两个向量进行混洗,很容易做类似的事情
whatever_rng_type rng2(rng1);
std::shuffle(vec1.begin(),vec1.end(),rng1);
std::shuffle(vec2.begin(),vec2.end(),rng2);
其中两个洗牌使用相同的RNG状态。但是,我没有看到任何要求这些混洗实际上在我检查过的standard draft中产生相同顺序的要求。
std::shuffle
必须使用所提供的RNG作为其随机性源,但是该实现也可能会针对不同的元素大小执行类似的操作,例如采用不同的代码路径。也许某个实现可能对某些类型使用AVX512收集/分散指令,而对其他类型使用通用的非矢量化代码路径,这可能会影响结果的排序。
使用相同的种子执行两次混洗实际上是获得相同订单的安全方法吗?在更高的标准版本中是否缺少某些内容,或者是否存在缺陷报告?
解决方法
我仔细检查了该标准,并同意没有任何要求改组才能保证使用随机数的任何保证。它只是说:
“在此函数的实现使用随机数的范围内,由g引用的对象应作为实现的随机性来源。”
因此,剩下的问题似乎是您是否对 任何特定的实现定义或观察到的行为 实施,或将坚持使用符合标准的便携式计算机 解决方案(例如改组代理对象或使用一组 索引)...?
根据您的评论,您似乎反对索引数组的建议,因此在下面-一种使用自定义迭代器和代理对向量进行随机整理的实现...
(做得不是很仔细-只是概念/插图的证明,因此在使用任何重要内容之前,请仔细检查...。)
该方法需要一个move_together
对象,该对象保留对向量的引用,然后传递shuffle
iterator
个对象,这些对象具有指向move_together
对象的指针和一个索引。正在处理的向量。您可以通过在move_together
对象前面加上直接在迭代器中具有两个矢量的指针或引用来简化此过程。取消迭代器的引用后,它们将返回代理对象,然后支持swap
ping。
表面上它可以与GCC 10.2和clang 10一起使用,但是对于需要更充实的迭代器或代理的另一个编译器,std::shuffle
可能会有不同的实现。
#include <iostream>
#include <vector>
#include <random>
#include <string>
#include <algorithm>
template <typename T1,typename T2>
struct move_together;
template <typename T1,typename T2>
struct proxy
{
const move_together<T1,T2>* p_;
const size_t i_;
proxy& operator=(const proxy& rhs);
};
template <typename T1,typename T2>
struct move_together
{
move_together(std::vector<T1>& v1,std::vector<T2>& v2)
: v1_(v1),v2_(v2)
{ }
struct iterator
{
using iterator_category = std::random_access_iterator_tag;
using difference_type = ssize_t;
using value_type = proxy<T1,T2>;
using pointer = value_type*;
using reference = value_type&;
const move_together* p_;
size_t i_;
value_type operator*() { return {p_,i_}; }
bool operator==(const iterator& rhs) const { return i_ == rhs.i_; }
bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
difference_type operator-(const iterator& rhs) const
{ return i_ - rhs.i_; }
iterator operator+(int distance) const
{ return {p_,i_ + distance}; }
iterator operator++(int) { auto x = *this; ++i_; return x; }
iterator& operator++() { ++i_; return *this; }
};
iterator begin() { return {this,0}; }
iterator end() { return {this,std::min(v1_.size(),v2_.size())}; }
std::vector<T1>& v1_;
std::vector<T2>& v2_;
};
template <typename T1,typename T2>
proxy<T1,T2>& proxy<T1,T2>::operator=(const proxy<T1,T2>& rhs)
{
p_->v1_[i_] = rhs.p_->v1_[rhs.i_];
p_->v2_[i_] = rhs.p_->v2_[rhs.i_];
}
template <typename T1,typename T2>
void swap(proxy<T1,T2> lhs,proxy<T1,T2> rhs) {
using std::swap;
swap(lhs.p_->v1_[lhs.i_],rhs.p_->v1_[rhs.i_]);
swap(lhs.p_->v2_[lhs.i_],rhs.p_->v2_[rhs.i_]);
}
int main()
{
std::vector<int> v1{ {1,2,3,4,5} };
std::vector<std::string> v2{ {"one","two","three","four","five"} };
std::random_device rd;
std::mt19937 rng{rd()};
move_together m{v1,v2};
std::shuffle(m.begin(),m.end(),rng);
for (const auto& x : v1) std::cout << x << '/';
std::cout << '\n';
for (const auto& x : v2) std::cout << x << '/';
std::cout << '\n';
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。