2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-08-19 22:27:51 +02:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-12 12:21:58 +01:00
|
|
|
#define HAVE_IRCD_RAND_H
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Some character set dictionaries
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::rand::dict
|
2017-08-19 22:27:51 +02:00
|
|
|
{
|
|
|
|
extern const std::string alnum;
|
|
|
|
extern const std::string alpha;
|
|
|
|
extern const std::string upper;
|
|
|
|
extern const std::string lower;
|
|
|
|
extern const std::string numeric;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Tools for randomization
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::rand
|
|
|
|
{
|
2017-09-12 18:37:44 +02:00
|
|
|
extern std::random_device device;
|
|
|
|
extern std::mt19937_64 mt;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
uint64_t integer();
|
2018-08-23 02:19:18 +02:00
|
|
|
uint64_t integer(const uint64_t &min, const uint64_t &max); // inclusive
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
// Random character from dictionary
|
|
|
|
char character(const std::string &dict = dict::alnum);
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2018-02-15 21:53:00 +01:00
|
|
|
// Random string from dictionary
|
|
|
|
string_view string(const std::string &dict, const mutable_buffer &out);
|
2017-08-28 23:51:22 +02:00
|
|
|
std::string string(const std::string &dict, const size_t &len);
|
|
|
|
}
|
2017-08-19 22:27:51 +02:00
|
|
|
|
|
|
|
// Random character from dictionary
|
|
|
|
inline char
|
|
|
|
ircd::rand::character(const std::string &dict)
|
|
|
|
{
|
|
|
|
assert(!dict.empty());
|
|
|
|
return dict.at(integer(0, dict.size() - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
ircd::rand::integer(const uint64_t &min,
|
|
|
|
const uint64_t &max)
|
|
|
|
{
|
|
|
|
std::uniform_int_distribution<uint64_t> dist{min, max};
|
|
|
|
return dist(mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
ircd::rand::integer()
|
|
|
|
{
|
|
|
|
return mt();
|
|
|
|
}
|