random_shuffle 算法 - 没有随机生成器函数会产生相同的结果吗?

发布于 2024-11-28 14:04:18 字数 224 浏览 4 评论 0原文

如果标准库中的 random_shuffle 算法没有提供随机生成器函数,如果提供相同的数据,程序的连续运行是否会产生相同的随机序列?

例如,如果

std::random_shuffle(filenames.begin(), filenames.end());

在程序的连续运行中对目录中的相同文件名列表执行,则生成的随机序列是否与先前运行中的相同?

If a random generator function is not supplied to the random_shuffle algorithm in the standard library, will successive runs of the program produce the same random sequence if supplied with the same data?

For example, if

std::random_shuffle(filenames.begin(), filenames.end());

is performed on the same list of filenames from a directory in successive runs of the program, is the random sequence produced the same as that in the prior run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红焚 2024-12-05 14:04:18

如果您使用相同的随机生成器、相同的种子和相同的起始
顺序,结果将是相同的。毕竟,计算机是
其行为具有确定性(模线程问题和其他一些问题)
什物)。

如果不指定生成器,则默认生成器是
定义的实现。我认为大多数实现都使用
std::rand() (这可能会导致问题,特别是当
序列中的元素大于RAND_MAX)。我会推荐
获得一台质量已知的发电机并使用它。

如果您没有正确播种正在使用的生成器(另一个
不使用默认值的原因,因为你如何播种它取决于
实施),然后你就会得到你得到的。如果是
std::rand(),默认始终使用相同的种子。你如何播种
取决于所使用的发电机。你用来播种的东西应该有所不同
从一次跑到另一次;对于许多应用程序,time(NULL)
充足的;在 Unix 平台上,我建议读取任意字节
它取自/dev/random。否则,散列其他信息(IP
机器地址、进程 ID 等)也可以改进——它
意味着两个用户在同一秒启动该程序
仍然会得到不同的序列。 (但这实际上只相关
如果您在网络环境中工作。)

If you use the same random generator, with the same seed, and the same starting
sequence, the results will be the same. A computer is, after all,
deterministic in its behavior (modulo threading issues and a few other
odds and ends).

If you do not specify a generator, the default generator is
implementation defined. Most implementations, I think, use
std::rand() (which can cause problems, particularly when the number of
elements in the sequence is larger than RAND_MAX). I would recommend
getting a generator with known quality, and using it.

If you don't correctly seed the generator which is being used (another
reason to not use the default, since how you seed it will depend on the
implementation), then you'll get what you get. In the case of
std::rand(), the default always uses the same seed. How you seed
depends on the generator used. What you use to seed it should be vary
from one run to the other; for many applications, time(NULL) is
sufficient; on a Unix platform, I'd recommend reading however many bytes
it takes from /dev/random. Otherwise, hashing other information (IP
address of the machine, process id, etc.) can also improve things---it
means that two users starting the program at exactly the same second
will still get different sequences. (But this is really only relevant
if you're working in a networked environment.)

海拔太高太耀眼 2024-12-05 14:04:18

25.2.11 只是说元素是均匀分布的。它不保证幕后使用哪个 RNG(除非您传入一个),因此您不能依赖任何此类行为。

为了保证相同的洗牌结果,您需要提供自己的 RNG 来提供这些保证,但我怀疑即使如此,如果您更新标准库,random_shuffle 算法本身也可能会改变效果。

25.2.11 just says that the elements are shuffled with uniform distribution. It makes no guarantees as to which RNG is used behind the scenes (unless you pass one in) so you can't rely on any such behavior.

In order to guarantee the same shuffle outcome you'll need to provide your own RNG that provides those guarantees, but I suspect even then if you update your standard library the random_shuffle algorithm itself could change effects.

旧街凉风 2024-12-05 14:04:18

每次运行程序都可能产生相同的结果。如果存在问题,您可以添加自定义随机数生成器(可以从外部源播种)作为 std::random_shuffle 的附加参数。该函数将是第三个参数。有些人建议random_shuffle之前调用srand(unsigned(time(NULL)));,但结果通常是实现定义的(并且< em>不可靠)。

You may produce an identical result every run of the program. You can add a custom random number generator (which can be seeded from an external source) as an additional argument to std::random_shuffle if this is a problem. The function would be the third argument. Some people recommend call srand(unsigned(time(NULL))); before random_shuffle, but the results are often times implementation defined (and unreliable).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文