summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-01-17 17:30:36 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-01-17 17:30:36 +0900
commit9b41970134706f598ede204502bbd196eeedb26c (patch)
treea57f0ef2c34f62e5c26e037870f8af666f8f0035 /src
parent0282899bfa41e49caebde19432b53570b14259c9 (diff)
Don't use std::random_device directly as suggested by document
See http://en.cppreference.com/w/cpp/numeric/random/random_device
Diffstat (limited to 'src')
-rw-r--r--src/SimpleRandomizer.cc12
-rw-r--r--src/SimpleRandomizer.h4
2 files changed, 10 insertions, 6 deletions
diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc
index 11ce13d6..6a65d5f7 100644
--- a/src/SimpleRandomizer.cc
+++ b/src/SimpleRandomizer.cc
@@ -63,7 +63,11 @@ const std::unique_ptr<SimpleRandomizer>& SimpleRandomizer::getInstance()
return randomizer_;
}
-SimpleRandomizer::SimpleRandomizer()
+namespace {
+std::random_device rd;
+} // namespace
+
+SimpleRandomizer::SimpleRandomizer() : gen_(rd())
{
#ifdef __MINGW32__
BOOL r = ::CryptAcquireContext(&provider_, 0, 0, PROV_RSA_FULL,
@@ -120,12 +124,12 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
#endif // defined(HAVE_GETRANDOM_INTERFACE)
auto ubuf = reinterpret_cast<result_type*>(buf);
size_t q = len / sizeof(result_type);
- auto gen = std::uniform_int_distribution<result_type>();
+ auto dis = std::uniform_int_distribution<result_type>();
for (; q > 0; --q, ++ubuf) {
- *ubuf = gen(dev_);
+ *ubuf = dis(gen_);
}
const size_t r = len % sizeof(result_type);
- auto last = gen(dev_);
+ auto last = dis(gen_);
memcpy(ubuf, &last, r);
#endif // ! __MINGW32__
}
diff --git a/src/SimpleRandomizer.h b/src/SimpleRandomizer.h
index 879b2a06..7440dd36 100644
--- a/src/SimpleRandomizer.h
+++ b/src/SimpleRandomizer.h
@@ -55,11 +55,11 @@ private:
#ifdef __MINGW32__
HCRYPTPROV provider_;
#else
- std::random_device dev_;
+ std::mt19937 gen_;
#endif // ! __MINGW32__
public:
- typedef std::random_device::result_type result_type;
+ typedef std::mt19937::result_type result_type;
static const std::unique_ptr<SimpleRandomizer>& getInstance();