在O(1)内存中的随机序列迭代?

编程入门 行业动态 更新时间:2024-10-12 05:47:47
本文介绍了在O(1)内存中的随机序列迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设您要以随机顺序迭代序列[0到n],每次访问每个元素一次。有没有办法在 (1)内存,即不使用 std :: iota 创建[1..n]并通过 std :: random_shuffle ?

Say you want to iterate over a sequence [0 to n] in a random order, visiting every element exactly once. Is there any way to do this in O(1) memory, i.e. without creating an [1..n] sequence with std::iota and running it through std::random_shuffle?

运行它。某种迭代器以随机顺序排列序列

Some kind of iterator spitting out the sequence in a random order would be optimal.

要求是可以通过选择另一个种子来获得另一个随机顺序。

A requirement is that it should be possible to get another random order by picking another seed.

推荐答案

在理论上,如果你构建一个随机数生成器,其周期恰好为 n ,并覆盖0..n中的所有值,给你你喜欢什么。

In theory, if you built a random number generator whose period was exactly n, and covered all values in 0..n, then running through this once would give you what you like.

当然,这可能不是一个一般的解决方案,至少如果你正在寻找一些动态,因为你必须预先创建PRNG和你如何做这取决于n。

Of course, this may not be a general solution, at least if you are looking for something dynamic, since you would have to pre-create the PRNG and how you do this depends on n.

更多推荐

在O(1)内存中的随机序列迭代?

本文发布于:2023-11-29 22:39:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647836.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   内存   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!