Index: i686-w64-mingw32/include/c++/4.8.0/bits/stl_algo.h =================================================================== --- i686-w64-mingw32/include/c++/4.8.0/bits/stl_algo.h (revision 94918) +++ i686-w64-mingw32/include/c++/4.8.0/bits/stl_algo.h (working copy) @@ -5214,9 +5214,21 @@ _RandomAccessIterator>) __glibcxx_requires_valid_range(__first, __last); - if (__first != __last) - for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) - std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1))); + if (__first != __last) { + // Bugfix: std::rand() may provide "small" numbers only and indeed it + // does when using Windows libc (x < 32768). + // We initialize and use a xorshift implementation seeded by + // a couple of calls to rand() instead of using rand() for all + // the random numbers needed. + unsigned __xss = (unsigned)std::rand() ^ ((unsigned)std::rand()<<15); + __xss += !__xss; + for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) { + __xss ^= __xss << 13; + __xss ^= __xss >> 17; + __xss ^= __xss << 5; + std::iter_swap(__i, __first + (__xss % ((__i - __first) + 1))); + } + } } /** Index: i686-w64-mingw32/include/c++/4.8.0/ext/algorithm =================================================================== --- i686-w64-mingw32/include/c++/4.8.0/ext/algorithm (revision 94918) +++ i686-w64-mingw32/include/c++/4.8.0/ext/algorithm (working copy) @@ -276,8 +276,18 @@ _Distance __remaining = std::distance(__first, __last); _Distance __m = min(__n, __remaining); + // Bugfix: std::rand() may provide "small" numbers only and indeed it + // does when using Windows libc (x < 32768). + // We initialize and use a xorshift implementation seeded by + // a couple of calls to rand() instead of using rand() for all + // the random numbers needed. + unsigned __xss = (unsigned)std::rand() ^ ((unsigned)std::rand()<<15); + __xss += !__xss; while (__m > 0) { - if ((std::rand() % __remaining) < __m) + __xss ^= __xss << 13; + __xss ^= __xss >> 17; + __xss ^= __xss << 5; + if ((__xss % __remaining) < __m) { *__out = *__first; ++__out;