shill: replace rand() with std::default_random_engine
Replace rand() with STL random number generator to have
superior quality of random numbers, thread safety and
stop linter from complaining.
BUG=None
TEST=Unit tests pass.
Change-Id: I80da776c70bbd8bffe10916554393621ad60131e
Reviewed-on: https://chromium-review.googlesource.com/204430
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/default_profile.cc b/default_profile.cc
index c17c63f..1a7d828 100644
--- a/default_profile.cc
+++ b/default_profile.cc
@@ -4,6 +4,7 @@
#include "shill/default_profile.h"
+#include <random>
#include <vector>
#include <base/files/file_path.h>
@@ -62,7 +63,8 @@
: Profile(control, metrics, manager, Identifier(profile_id), "", true),
storage_path_(storage_path),
profile_id_(profile_id),
- props_(manager_props) {
+ props_(manager_props),
+ random_engine_(time(nullptr)) {
PropertyStore *store = this->mutable_store();
store->RegisterConstBool(kArpGatewayProperty, &manager_props.arp_gateway);
store->RegisterConstString(kCheckPortalListProperty,
@@ -117,8 +119,8 @@
}
if (!storage()->GetInt(kStorageId, kStorageConnectionIdSalt,
&manager_props->connection_id_salt)) {
- srand(time(NULL));
- manager_props->connection_id_salt = rand(); // NOLINT - rand
+ manager_props->connection_id_salt =
+ std::uniform_int_distribution<int>()(random_engine_);
}
}
diff --git a/default_profile.h b/default_profile.h
index 60125fa..afae425 100644
--- a/default_profile.h
+++ b/default_profile.h
@@ -5,6 +5,7 @@
#ifndef SHILL_DEFAULT_PROFILE_H_
#define SHILL_DEFAULT_PROFILE_H_
+#include <random>
#include <string>
#include <vector>
@@ -84,6 +85,7 @@
const base::FilePath storage_path_;
const std::string profile_id_;
const Manager::Properties &props_;
+ std::default_random_engine random_engine_;
DISALLOW_COPY_AND_ASSIGN(DefaultProfile);
};
diff --git a/ip_address_store.cc b/ip_address_store.cc
index 67c759e..eee5655 100644
--- a/ip_address_store.cc
+++ b/ip_address_store.cc
@@ -21,8 +21,7 @@
return lhs.ToString() < rhs.ToString();
}
-IPAddressStore::IPAddressStore() {
- srand(time(NULL));
+IPAddressStore::IPAddressStore() : random_engine_(time(nullptr)) {
}
IPAddressStore::~IPAddressStore() {}
@@ -46,7 +45,8 @@
IPAddress IPAddressStore::GetRandomIP() {
if (ip_addresses_.empty())
return IPAddress(IPAddress::kFamilyUnknown);
- int index = rand() % ip_addresses_.size(); // NOLINT(runtime/threadsafe_fn)
+ std::uniform_int_distribution<int> uniform_rand(0, ip_addresses_.size() - 1);
+ int index = uniform_rand(random_engine_);
IPAddresses::const_iterator cit = ip_addresses_.begin();
advance(cit, index);
return *cit;
diff --git a/ip_address_store.h b/ip_address_store.h
index fd3ee04..966082e 100644
--- a/ip_address_store.h
+++ b/ip_address_store.h
@@ -5,6 +5,7 @@
#ifndef SHILL_IP_ADDRESS_STORE_H_
#define SHILL_IP_ADDRESS_STORE_H_
+#include <random>
#include <set>
#include "shill/ip_address.h"
@@ -43,6 +44,7 @@
private:
IPAddresses ip_addresses_;
+ std::default_random_engine random_engine_;
DISALLOW_COPY_AND_ASSIGN(IPAddressStore);
};