随机长生成器C ++ [英] Random long long generator C++
问题描述
什么是生成随机 long long
和加密强度С++的解决方案? (允许提升)
What is a solution to generate random long long
with cryptographic strength С++? (boost is allowed)
推荐答案
< random>
便携式访问随机数设施,包括可能的加密pRNG。
The <random>
header provides portable access to random number facilities including, potentially, a cryptographic pRNG.
#include <random> // random_device, uniform_int_distribution
#include <algorithm> // generate_n
#include <iterator> // ostream_iterator
#include <iostream> // cout
#include <functional> // bind, ref
int main() {
std::random_device r;
std::uniform_int_distribution<long long> dist;
std::generate_n(std::ostream_iterator<long long>(std::cout, "\n"), 10,
std::bind(dist,std::ref(r)));
}
std :: random_device
可能不是所有实现上的加密pRNG,因此您必须检查实现文档。特别是2012年之前的VC ++不提供非确定性的实现。
std::random_device
may not be a cryptographic pRNG on all implementations, so you'll have to check your implementation documentation. In particular VC++ before 2012 does not provide a non-deterministic implementation. VC++ 2012 and later implements this using Windows cryptography services.
其他操作系统(如Linux或Mac OS X)上的实现通常可以使用/ dev / urandom dev / random或通过文件系统公开的任何其他随机设备。例如。 libc ++默认使用/ dev / urandom,在OS X上使用Yarrow算法。
Implementations on other operating systems such as Linux or Mac OS X can commonly use "/dev/urandom" or "/dev/random" or any other random device exposed via the filesystem. E.g. libc++ uses "/dev/urandom" by default, which on OS X uses the Yarrow algorithm.
我知道你排除了boost,但 boost :: random_device
具有使用该平台的加密服务的Windows实现。
I know you ruled out boost, but boost::random_device
has an implementation for Windows that uses that platform's cryptography services.
这篇关于随机长生成器C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!