是< random>库在c ++ 11可移植? [英] Is the <random> library in c++11 portable?
问题描述
库是否在c ++ 11可移植?
Is the library in c++11 portable? I have avoided rand() because I heard it wasn't portable.
推荐答案
如何定义可移植?
如果通过portable,你的意思是将产生相同输入的随机数的二进制相同序列,则是, rand
不可移植。是的, C ++随机数生成器是可移植的(大多数不是 std :: default_random_engine
或 std :: random_device
),因为它们实现了特定算法。 rand
可以是任何东西,只要它不完全不同于随机数字生成器。
If by "portable", you mean "will produce binary identical sequences of random numbers given the same input", then yes, rand
isn't portable. And yes, the C++ random generators are portable (most of them. Not std::default_random_engine
or std::random_device
), because they implement specific algorithms. rand
is allowed to be anything, as long as it's not entirely unlike a random number generator.
,正如@PeteBecker指出的,分布本身没有这么好定义。因此, std :: mt19937
会为给定的种子产生相同的值序列,不同 std :: uniform_int_distribution
可以为同一输入序列和范围提供不同的值。
That being said, as @PeteBecker pointed out, the distributions themselves are not so well-defined. So while std::mt19937
will produce the same sequence of values for a given seed, different std::uniform_int_distribution
s can give different values for the same input sequence and range.
当然,如果你需要一致性,你总是可以定义自己的分布。
Of course, if you need consistency, you can always define your own distribution.
这篇关于是< random>库在c ++ 11可移植?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!