Is the library in c++11 portable? I have avoided rand() because I heard it wasn't portable.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
How do you define "portable"?
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. Notstd::default_random_engine
orstd::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.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, differentstd::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.
The random number engines described in
<random>
have explicit requirements for their algorithms to ensure portability. The distributions do not.You can generate "identical sequences of random numbers given the same input" (from @Nicol Bolas) with std::mt19937 (Mersenne Twister) for example. You definitely couldn't do that with
rand()
which was quite annoying.Related questions:
Does the C++11 standard guarantee identical random numbers for the same seed across implementations?
Consistent pseudo-random numbers across platforms