C / C ++算法,以从不同的平台上相同的种子相同的伪随机数序列? [英] C/C++ algorithm to produce same pseudo-random number sequences from same seed on different platforms?

查看:172
本文介绍了C / C ++算法,以从不同的平台上相同的种子相同的伪随机数序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切,我pferably独立,因为我不希望添加更多的库寻找的东西$ P $。

The title says it all, I am looking for something preferably stand-alone because I don't want to add more libraries.

因为我需要它在紧张的高性能回路性能应该不错。我想这会在随机程度的成本。

Performance should be good since I need it in a tight high-performance loop. I guess that will come at a cost of the degree of randomness.

推荐答案

任何特定的伪随机数生成算法将这样的表现。与兰特的问题是,它没有规定它是如何实现的。不同实现的行为以不同的方式,甚至有不同的品质。

Any particular pseudo-random number generation algorithm will behave like this. The problem with rand is that it's not specified how it is implemented. Different implementations will behave in different ways and even have varying qualities.

不过,C ++ 11提供了新的 <随机> 的标准库头包含很多伟大的随机数生成设施。内定义的随机数引擎是明确定义和,给予相同的种子,总是会产生相同的一组数字。

However, C++11 provides the new <random> standard library header that contains lots of great random number generation facilities. The random number engines defined within are well-defined and, given the same seed, will always produce the same set of numbers.

例如,一个流行的高品质的随机数引擎的std :: mt19937 ,这是一种特定的方式配置的梅森捻线机算法。无论哪台机器,你在,下面总是会产生0和1之间的相同的一组实数:

For example, a popular high quality random number engine is std::mt19937, which is the Mersenne twister algorithm configured in a specific way. No matter which machine, you're on, the following will always produce the same set of real numbers between 0 and 1:

std::mt19937 engine(0); // Fixed seed of 0
std::uniform_real_distribution<> dist;
for (int i = 0; i < 100; i++) {
  std::cout << dist(engine) << std::endl;
}

这篇关于C / C ++算法,以从不同的平台上相同的种子相同的伪随机数序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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