Skip to content

Commit

Permalink
Random number generation improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSuprunenko committed Feb 11, 2019
1 parent bfb80b4 commit 5d39440
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 1 addition & 2 deletions include/lib/graft/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ std::string base64_encode(const std::string &data);
template <typename T>
T random_number(T startRange, T endRange)
{
std::random_device rd;
std::mt19937 mt(rd());
static std::mt19937 mt(std::random_device{}());
std::uniform_int_distribution<T> dist(startRange, endRange);
return dist(mt);
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/graft/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "lib/graft/handler_api.h"
#include "lib/graft/expiring_list.h"
#include "lib/graft/sys_info.h"
#include "lib/graft/common/utils.h"

#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "supernode.task"
Expand Down Expand Up @@ -981,8 +982,9 @@ std::chrono::milliseconds PeriodicTask::getTimeout()
return m_initial_timeout_ms;
}
if(m_random_factor < 0.0001) return m_timeout_ms;

return std::chrono::milliseconds( int(m_timeout_ms.count() * (1.0 + std::rand()*m_random_factor/RAND_MAX) ));
using i_type = decltype(m_timeout_ms.count());
i_type v = graft::utils::random_number(m_timeout_ms.count(), (i_type)(m_timeout_ms.count()*(1.0 + m_random_factor)));
return std::chrono::milliseconds(v);
}

ClientTask::ClientTask(ConnectionManager* connectionManager, mg_connection *client, Router::JobParams& prms)
Expand Down

0 comments on commit 5d39440

Please sign in to comment.