I understand that the rand()
function generates pseudo-random numbers based on the seed it is given, and that on a given platform it will always generate the same sequence of numbers from the same seed, what I want to know is why it gives a different sequence across platforms that use the same library? I.e. how is rand()
implemented?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
This is what that IEEE Std 1003.1 C-standard says about how the
rand()
function should behave. It doesn't say anything about how the sequence should be calculated. In other words, each implementer is free to choose their own version of a pseudo random sequence generator.Your observations shows that they have taken advantage of that freedom.
I might also point out that
rand()
is a part of the<cstdlib>
which is more or less a copy of the C standard library , and the new library will provide you with more flexibility and standard sequence generators if you have a new enough C++ compiler and doesn't depend on C - C++ interoperability.The C++ standard does not specify what algorithm is used for the rand() function.
The functionality is defined by whoever wrote the standard library on your system: Microsoft for the standard library included with Visual Studio, and the GNU guys for the standard library packaged with GCC.
Your compiler is making the choice of where it gets its library from, so you may have different versions of the standard library for different compilers on the same system. The point remains the same: the specification guarantees what functions are available and what they do; not how they do it.