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
{
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/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

View File

@ -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;
}

View File

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

View File

@ -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.

View File

@ -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<torrent_peer const*>::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<int, int>::first, _1) <
boost::bind(&std::pair<int, int>::first, _2));

View File

@ -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);
}
}