msvc support
[m6w6/libmemcached] / src / bin / common / random.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "time.hpp"
19 #include <random>
20 #undef max
21
22 class random64 {
23 public:
24 using typ = std::mt19937_64::result_type;
25
26 random64()
27 : gen{static_cast<typ>(time_clock::now().time_since_epoch().count())}
28 , dst{}
29 {}
30
31 typ operator()(typ min_ = 0, typ max_ = std::numeric_limits<typ>::max()) {
32 return (dst(gen) % (max_ - min_)) + min_;
33 }
34
35 void fill(char *buf, size_t len,
36 const std::string &set = "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz") {
37 for (auto i = 0ul; i < len; ++i) {
38 buf[i] = set[(*this)(0, set.length()-1)];
39 }
40 }
41
42 private:
43 std::mt19937_64 gen;
44 std::uniform_int_distribution<typ> dst;
45 };