rand() 的结果在各个系统之间的可预测性如何? [英] How predictable is the result of rand() between individual systems?

查看:29
本文介绍了rand() 的结果在各个系统之间的可预测性如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划创建一个包含可预测随机性的挑战.我知道对于给定的系统,如果 srand() 使用相同的值进行播种,rand() 将返回可预测的结果序列.但是,我不确定这个序列在不同系统之间的一致性程度.只要编译器使用相同的实现,结果是否相同?还是同一个程序在两个不同的系统上会产生不同的结果?

I plan to create a challenge which will involve predictable randomness. I understand that for a given system, if srand() is seeded using the same value, rand() will return a predictable sequence of results. However I’m not sure to what extent this sequence is consistent between different systems. Will the result be the same as long as the compiler uses the same implementation? Or will the same program on two different systems yield different results?

推荐答案

如果您只是想要良好的统计属性而不需要任何加密问题,那么随机数生成器 (RNG) 的代码并不多.在您的程序中包含 RNG 的代码.那么无论它在哪里运行,它都会是相同的序列.

A random number generator (RNG) is not much code if you simply want good statistical properties without any cryptographic concerns. Include the code for the RNG in your program. Then it will be the same sequence wherever it's run.

考虑来自 PCG 系列Xoshiro.M.E.O'Neill 的博客 有几篇关于通过 PractRand 和 TestU01 统计测试的小型 RNG 的帖子,例如 Bob Jenkins's Small64-bit 最小标准生成器 -- 这些只是几行代码!这是一个例子:

Consider something from the PCG family or Xoshiro. M.E. O'Neill's blog has several posts on small RNGs that pass PractRand and TestU01 statistical tests, like Bob Jenkins's Small and 64-bit Minimal Standard generators -- these are just a few lines of code! Here's an example :

uint128_t state = 1;   // can be seeded to any odd number

uint64_t next()
{
    state *= 0x0fc94e3bf4e9ab32866458cd56f5e605;
            // Spectral test: M8 = 0.71005, M16 = 0.66094, M24 = 0.61455
    return state >> 64;
}

这篇关于rand() 的结果在各个系统之间的可预测性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆