travis: fix clang
[awesomized/libmemcached] / testing / lib / random.hpp
index 208b09f43daac11ff5965a24e3471bff939424d5..19ff4f8b32700a24c125ff4bcc8912d6a84aeaf4 100644 (file)
@@ -1,6 +1,8 @@
 #pragma once
 
 #include <cstddef>
+#include <chrono>
+#include <random>
 #include <string>
 #include <type_traits>
 #include <utility>
@@ -10,9 +12,19 @@ using namespace std;
 using kv_pair = pair<string, string>;
 
 template<typename T>
-enable_if_t<is_integral_v<T>, T> random_num(T min, T max);
+enable_if_t<is_integral_v<T>, T> random_num(T min, T max) {
+  using namespace chrono;
+  using rnd = mt19937;
+  using dst = uniform_int_distribution<T>;
+
+  auto time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch());
+  auto seed = static_cast<rnd::result_type>(time.count() % numeric_limits<T>::max());
+  auto rgen = rnd{seed};
+  return dst(min, max)(rgen);
+}
 
 unsigned random_port();
+string random_port_string(const string &);
 
 char random_ascii(char min = '!', char max = '~');
 string random_ascii_string(size_t len, char min = '!', char max = '~');