diff --git a/include/libtorrent/random.hpp b/include/libtorrent/random.hpp index b8d427a99..d33d1a1cc 100644 --- a/include/libtorrent/random.hpp +++ b/include/libtorrent/random.hpp @@ -36,4 +36,5 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { boost::uint32_t TORRENT_EXTRA_EXPORT random(); + boost::uint32_t TORRENT_EXTRA_EXPORT randint(int i); } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index a037b5018..c6f7c6545 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/resolver_interface.hpp" #include "libtorrent/settings_pack.hpp" #include "libtorrent/aux_/time.hpp" +#include "libtorrent/random.hpp" #if defined TORRENT_ASIO_DEBUGGING #include "libtorrent/debug.hpp" @@ -552,7 +553,7 @@ void http_connection::on_resolve(error_code const& e return; } - std::random_shuffle(m_endpoints.begin(), m_endpoints.end()); + std::random_shuffle(m_endpoints.begin(), m_endpoints.end(), randint); // The following statement causes msvc to crash (ICE). Since it's not // necessary in the vast majority of cases, just ignore the endpoint diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 421fd654d..793b22a74 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -1564,7 +1564,7 @@ namespace libtorrent , end(m_priority_boundries.end()); i != end; ++i) { if (start == *i) continue; - std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + *i); + std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + *i, randint); start = *i; } diff --git a/src/random.cpp b/src/random.cpp index 8b76164a0..f2e91bbbc 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -94,5 +94,10 @@ namespace libtorrent #endif // TORRENT_BUILD_SIMULATOR + boost::uint32_t randint(int i) + { + return random() % i; + } + } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 7d53751b8..451e2a024 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3768,7 +3768,7 @@ retry: // unchoked // avoid having a bias towards peers that happen to be sorted first - std::random_shuffle(opt_unchoke.begin(), opt_unchoke.end()); + std::random_shuffle(opt_unchoke.begin(), opt_unchoke.end(), randint); // sort all candidates based on when they were last optimistically // unchoked. diff --git a/src/torrent.cpp b/src/torrent.cpp index d939d762f..84622821b 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -7305,7 +7305,7 @@ namespace libtorrent // if we didn't save 100 peers, fill in with second choice peers if (num_saved_peers < 100) { - std::random_shuffle(deferred_peers.begin(), deferred_peers.end()); + std::random_shuffle(deferred_peers.begin(), deferred_peers.end(), randint); for (std::vector::const_iterator i = deferred_peers.begin() , end(deferred_peers.end()); i != end && num_saved_peers < 100; ++i) { @@ -10339,7 +10339,7 @@ namespace libtorrent if (p->is_seed()) seeds.push_back(p); } - std::random_shuffle(seeds.begin(), seeds.end()); + std::random_shuffle(seeds.begin(), seeds.end(), randint); TORRENT_ASSERT(to_disconnect <= int(seeds.size())); for (int i = 0; i < to_disconnect; ++i) seeds[i]->disconnect(errors::upload_upload_connection @@ -10497,7 +10497,7 @@ namespace libtorrent --pieces[i->piece].first; } - std::random_shuffle(pieces.begin(), pieces.end()); + std::random_shuffle(pieces.begin(), pieces.end(), randint); std::stable_sort(pieces.begin(), pieces.end() , boost::bind(&std::pair::first, _1) < boost::bind(&std::pair::first, _2)); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index e1ee82368..c0e07054a 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/utf8.hpp" #include "libtorrent/time.hpp" +#include "libtorrent/random.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/aux_/escape_string.hpp" // maybe_url_encode @@ -1501,12 +1502,12 @@ namespace libtorrent { if (stop->tier != current_tier) { - std::random_shuffle(start, stop); + std::random_shuffle(start, stop, randint); start = stop; current_tier = stop->tier; } } - std::random_shuffle(start, stop); + std::random_shuffle(start, stop, randint); } }