summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/BoostRandomGenerator.cpp27
-rw-r--r--Swiften/Base/RandomGenerator.cpp4
-rw-r--r--Swiften/Base/SConscript2
-rw-r--r--Swiften/Base/StdRandomGenerator.cpp33
-rw-r--r--Swiften/Base/StdRandomGenerator.h (renamed from Swiften/Base/BoostRandomGenerator.h)10
5 files changed, 40 insertions, 36 deletions
diff --git a/Swiften/Base/BoostRandomGenerator.cpp b/Swiften/Base/BoostRandomGenerator.cpp
deleted file mode 100644
index d893d4d..0000000
--- a/Swiften/Base/BoostRandomGenerator.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2012-2016 Isode Limited.
- * All rights reserved.
- * See the COPYING file for more information.
- */
-
-#include <Swiften/Base/BoostRandomGenerator.h>
-
-#include <ctime>
-#include <numeric>
-
-#include <boost/random/uniform_int.hpp>
-#include <boost/random/variate_generator.hpp>
-
-namespace Swift {
-
-BoostRandomGenerator::BoostRandomGenerator() {
- // FIXME: Not a good seed
- generator.seed(static_cast<unsigned int>(std::time(nullptr)));
-}
-
-int BoostRandomGenerator::generateRandomInteger(int maximum) {
- boost::uniform_int<> distribution(0, maximum);
- return distribution(generator);
-}
-
-}
diff --git a/Swiften/Base/RandomGenerator.cpp b/Swiften/Base/RandomGenerator.cpp
index f0d8804..e43c01a 100644
--- a/Swiften/Base/RandomGenerator.cpp
+++ b/Swiften/Base/RandomGenerator.cpp
@@ -8,8 +8,6 @@
namespace Swift {
-RandomGenerator::~RandomGenerator() {
-
-}
+RandomGenerator::~RandomGenerator() = default;
}
diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript
index de502c2..1f48f19 100644
--- a/Swiften/Base/SConscript
+++ b/Swiften/Base/SConscript
@@ -1,7 +1,6 @@
Import("swiften_env")
objects = swiften_env.SwiftenObject([
- "BoostRandomGenerator.cpp",
"ByteArray.cpp",
"DateTime.cpp",
"Error.cpp",
@@ -16,6 +15,7 @@ objects = swiften_env.SwiftenObject([
"SafeAllocator.cpp",
"SafeByteArray.cpp",
"SimpleIDGenerator.cpp",
+ "StdRandomGenerator.cpp",
"String.cpp",
"URL.cpp",
"sleep.cpp",
diff --git a/Swiften/Base/StdRandomGenerator.cpp b/Swiften/Base/StdRandomGenerator.cpp
new file mode 100644
index 0000000..8b1dee0
--- /dev/null
+++ b/Swiften/Base/StdRandomGenerator.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012-2017 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swiften/Base/StdRandomGenerator.h>
+
+#include <algorithm>
+#include <functional>
+
+namespace {
+ template<class T = std::mt19937, std::size_t N = T::state_size>
+ typename std::enable_if<!!N, T>::type createSeededRandomEngine() {
+ typename T::result_type random_data[N];
+ std::random_device source;
+ std::generate(std::begin(random_data), std::end(random_data), std::ref(source));
+ std::seed_seq seeds(std::begin(random_data), std::end(random_data));
+ return T(seeds);
+ }
+}
+
+namespace Swift {
+
+StdRandomGenerator::StdRandomGenerator() : generator(createSeededRandomEngine()) {
+}
+
+int StdRandomGenerator::generateRandomInteger(int maximum) {
+ std::uniform_int_distribution<> distribution(0, maximum);
+ return distribution(generator);
+}
+
+}
diff --git a/Swiften/Base/BoostRandomGenerator.h b/Swiften/Base/StdRandomGenerator.h
index ff715c3..4cc5e95 100644
--- a/Swiften/Base/BoostRandomGenerator.h
+++ b/Swiften/Base/StdRandomGenerator.h
@@ -1,25 +1,25 @@
/*
- * Copyright (c) 2012-2016 Isode Limited.
+ * Copyright (c) 2012-2017 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <boost/random/mersenne_twister.hpp>
+#include <random>
#include <Swiften/Base/API.h>
#include <Swiften/Base/Override.h>
#include <Swiften/Base/RandomGenerator.h>
namespace Swift {
- class SWIFTEN_API BoostRandomGenerator : public RandomGenerator {
+ class SWIFTEN_API StdRandomGenerator : public RandomGenerator {
public:
- BoostRandomGenerator();
+ StdRandomGenerator();
int generateRandomInteger(int max) SWIFTEN_OVERRIDE;
private:
- boost::mt19937 generator;
+ std::mt19937 generator;
};
}