X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fbin%2Fcommon%2Frandom.hpp;fp=src%2Fbin%2Fcommon%2Frandom.hpp;h=eab844f1d3c449e6289cb273088d55c64d705936;hb=e1ba5b9e4eb179295274026ad8fd40a68eb6c67f;hp=0000000000000000000000000000000000000000;hpb=af58c4c6067fa41da3b4c8f01200065226070e37;p=awesomized%2Flibmemcached diff --git a/src/bin/common/random.hpp b/src/bin/common/random.hpp new file mode 100644 index 00000000..eab844f1 --- /dev/null +++ b/src/bin/common/random.hpp @@ -0,0 +1,44 @@ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#pragma once + +#include "time.hpp" +#include + +class random64 { +public: + using typ = std::mt19937_64::result_type; + + random64() + : gen{static_cast(time_clock::now().time_since_epoch().count())} + , dst{} + {} + + typ operator()(typ min = 0, typ max = std::numeric_limits::max()) { + return (dst(gen) % (max - min)) + min; + } + + void fill(char *buf, size_t len, + const std::string &set = "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz") { + for (auto i = 0ul; i < len; ++i) { + buf[i] = set[(*this)(0, set.length()-1)]; + } + } + +private: + std::mt19937_64 gen; + std::uniform_int_distribution dst; +};