std :: shuffle的顺序是否可以依赖RNG以外的任何内容?

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res