bin: consolidate clients
[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
21 class random64 {
22 public:
23 using typ = std::mt19937_64::result_type;
24
25 random64()
26 : gen{static_cast<typ>(time_clock::now().time_since_epoch().count())}
27 , dst{}
28 {}
29
30 typ operator()(typ min = 0, typ max = std::numeric_limits<typ>::max()) {
31 return (dst(gen) % (max - min)) + min;
32 }
33
34 void fill(char *buf, size_t len,
35 const std::string &set = "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz") {
36 for (auto i = 0ul; i < len; ++i) {
37 buf[i] = set[(*this)(0, set.length()-1)];
38 }
39 }
40
41 private:
42 std::mt19937_64 gen;
43 std::uniform_int_distribution<typ> dst;
44 };