summaryrefslogtreecommitdiffstats
path: root/examples/network/torrent/torrentclient.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/network/torrent/torrentclient.cpp')
-rw-r--r--examples/network/torrent/torrentclient.cpp22
1 files changed, 11 insertions, 11 deletions
diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp
index ba87924ff9..95232646ab 100644
--- a/examples/network/torrent/torrentclient.cpp
+++ b/examples/network/torrent/torrentclient.cpp
@@ -692,7 +692,7 @@ void TorrentClient::connectToPeers()
// Start as many connections as we can
while (!weighedPeers.isEmpty() && ConnectionManager::instance()->canAddConnection()
- && (qrand() % (ConnectionManager::instance()->maxConnections() / 2))) {
+ && (QRandomGenerator::global()->bounded(ConnectionManager::instance()->maxConnections() / 2))) {
PeerWireClient *client = new PeerWireClient(ConnectionManager::instance()->clientId(), this);
RateController::instance()->addSocket(client);
ConnectionManager::instance()->addConnection(client);
@@ -701,7 +701,7 @@ void TorrentClient::connectToPeers()
d->connections << client;
// Pick a random peer from the list of weighed peers.
- TorrentPeer *peer = weighedPeers.takeAt(qrand() % weighedPeers.size());
+ TorrentPeer *peer = weighedPeers.takeAt(QRandomGenerator::global()->bounded(weighedPeers.size()));
weighedPeers.removeAll(peer);
peer->connectStart = QDateTime::currentSecsSinceEpoch();
peer->lastVisited = peer->connectStart;
@@ -1114,7 +1114,7 @@ void TorrentClient::scheduleUploads()
}
if ((client->peerWireState() & PeerWireClient::ChokingPeer) == 0) {
- if ((qrand() % 10) == 0)
+ if ((QRandomGenerator::global()->bounded(10)) == 0)
client->abort();
else
client->chokePeer();
@@ -1128,7 +1128,7 @@ void TorrentClient::scheduleUploads()
// random peer to allow it to compete for a position among the
// downloaders. (This is known as an "optimistic unchoke".)
if (!allClients.isEmpty()) {
- PeerWireClient *client = allClients[qrand() % allClients.size()];
+ PeerWireClient *client = allClients[QRandomGenerator::global()->bounded(allClients.size())];
if (client->peerWireState() & PeerWireClient::ChokingPeer)
client->unchokePeer();
}
@@ -1189,7 +1189,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
piece = d->payloads.value(client);
if (!piece) {
QList<TorrentPiece *> values = d->pendingPieces.values();
- piece = values.value(qrand() % values.size());
+ piece = values.value(QRandomGenerator::global()->bounded(values.size()));
piece->inProgress = true;
d->payloads.insert(client, piece);
}
@@ -1246,14 +1246,14 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
++it;
}
if (!partialPieces.isEmpty())
- piece = partialPieces.value(qrand() % partialPieces.size());
+ piece = partialPieces.value(QRandomGenerator::global()->bounded(partialPieces.size()));
if (!piece) {
// Pick a random piece 3 out of 4 times; otherwise, pick either
// one of the most common or the least common pieces available,
// depending on the state we're in.
int pieceIndex = 0;
- if (d->state == WarmingUp || (qrand() & 4) == 0) {
+ if (d->state == WarmingUp || (QRandomGenerator::global()->generate() & 4) == 0) {
int *occurrences = new int[d->pieceCount];
memset(occurrences, 0, d->pieceCount * sizeof(int));
@@ -1293,7 +1293,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
}
// Select one piece randomly
- pieceIndex = piecesReadyForDownload.at(qrand() % piecesReadyForDownload.size());
+ pieceIndex = piecesReadyForDownload.at(QRandomGenerator::global()->bounded(piecesReadyForDownload.size()));
delete [] occurrences;
} else {
// Make up a list of available piece indices, and pick
@@ -1304,7 +1304,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
if (incompletePiecesAvailableToClient.testBit(i))
values << i;
}
- pieceIndex = values.at(qrand() % values.size());
+ pieceIndex = values.at(QRandomGenerator::global()->bounded(values.size()));
}
// Create a new TorrentPiece and fill in all initial
@@ -1396,8 +1396,8 @@ int TorrentClient::requestBlocks(PeerWireClient *client, TorrentPiece *piece, in
// speedup comes from an increased chance of receiving
// different blocks from the different peers.
for (int i = 0; i < bits.size(); ++i) {
- int a = qrand() % bits.size();
- int b = qrand() % bits.size();
+ int a = QRandomGenerator::global()->bounded(bits.size());
+ int b = QRandomGenerator::global()->bounded(bits.size());
int tmp = bits[a];
bits[a] = bits[b];
bits[b] = tmp;