Merge pull request #259 from arvidn/deterministic-rand

use a consistent random number generator
This commit is contained in:
Arvid Norberg 2015-11-12 21:45:09 -05:00
commit ef1f399fd3
7 changed files with 16 additions and 8 deletions

View File

@ -36,4 +36,5 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent namespace libtorrent
{ {
boost::uint32_t TORRENT_EXTRA_EXPORT random(); boost::uint32_t TORRENT_EXTRA_EXPORT random();
boost::uint32_t TORRENT_EXTRA_EXPORT randint(int i);
} }

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/resolver_interface.hpp" #include "libtorrent/resolver_interface.hpp"
#include "libtorrent/settings_pack.hpp" #include "libtorrent/settings_pack.hpp"
#include "libtorrent/aux_/time.hpp" #include "libtorrent/aux_/time.hpp"
#include "libtorrent/random.hpp"
#if defined TORRENT_ASIO_DEBUGGING #if defined TORRENT_ASIO_DEBUGGING
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
@ -552,7 +553,7 @@ void http_connection::on_resolve(error_code const& e
return; 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 // The following statement causes msvc to crash (ICE). Since it's not
// necessary in the vast majority of cases, just ignore the endpoint // necessary in the vast majority of cases, just ignore the endpoint

View File

@ -1564,7 +1564,7 @@ namespace libtorrent
, end(m_priority_boundries.end()); i != end; ++i) , end(m_priority_boundries.end()); i != end; ++i)
{ {
if (start == *i) continue; 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; start = *i;
} }

View File

@ -94,5 +94,10 @@ namespace libtorrent
#endif // TORRENT_BUILD_SIMULATOR #endif // TORRENT_BUILD_SIMULATOR
boost::uint32_t randint(int i)
{
return random() % i;
}
} }

View File

@ -3768,7 +3768,7 @@ retry:
// unchoked // unchoked
// avoid having a bias towards peers that happen to be sorted first // 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 // sort all candidates based on when they were last optimistically
// unchoked. // unchoked.

View File

@ -7305,7 +7305,7 @@ namespace libtorrent
// if we didn't save 100 peers, fill in with second choice peers // if we didn't save 100 peers, fill in with second choice peers
if (num_saved_peers < 100) 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<torrent_peer const*>::const_iterator i = deferred_peers.begin() for (std::vector<torrent_peer const*>::const_iterator i = deferred_peers.begin()
, end(deferred_peers.end()); i != end && num_saved_peers < 100; ++i) , 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); 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())); TORRENT_ASSERT(to_disconnect <= int(seeds.size()));
for (int i = 0; i < to_disconnect; ++i) for (int i = 0; i < to_disconnect; ++i)
seeds[i]->disconnect(errors::upload_upload_connection seeds[i]->disconnect(errors::upload_upload_connection
@ -10497,7 +10497,7 @@ namespace libtorrent
--pieces[i->piece].first; --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() std::stable_sort(pieces.begin(), pieces.end()
, boost::bind(&std::pair<int, int>::first, _1) < , boost::bind(&std::pair<int, int>::first, _1) <
boost::bind(&std::pair<int, int>::first, _2)); boost::bind(&std::pair<int, int>::first, _2));

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/file.hpp" #include "libtorrent/file.hpp"
#include "libtorrent/utf8.hpp" #include "libtorrent/utf8.hpp"
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/invariant_check.hpp" #include "libtorrent/invariant_check.hpp"
#include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/aux_/session_settings.hpp"
#include "libtorrent/aux_/escape_string.hpp" // maybe_url_encode #include "libtorrent/aux_/escape_string.hpp" // maybe_url_encode
@ -1501,12 +1502,12 @@ namespace libtorrent
{ {
if (stop->tier != current_tier) if (stop->tier != current_tier)
{ {
std::random_shuffle(start, stop); std::random_shuffle(start, stop, randint);
start = stop; start = stop;
current_tier = stop->tier; current_tier = stop->tier;
} }
} }
std::random_shuffle(start, stop); std::random_shuffle(start, stop, randint);
} }
} }