2018-02-03 18:22:01 -08: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 14:27:51 -06:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-12 03:21:58 -08:00
|
|
|
#define HAVE_IRCD_RAND_H
|
2017-08-19 14:27:51 -06:00
|
|
|
|
2017-09-12 09:37:44 -07:00
|
|
|
/// Some character set dictionaries
|
2017-08-28 14:51:22 -07:00
|
|
|
namespace ircd::rand::dict
|
2017-08-19 14:27:51 -06: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 09:37:44 -07:00
|
|
|
/// Tools for randomization
|
2017-08-28 14:51:22 -07:00
|
|
|
namespace ircd::rand
|
|
|
|
{
|
2020-10-06 15:46:36 -07:00
|
|
|
// Interface state.
|
2017-09-12 09:37:44 -07:00
|
|
|
extern std::random_device device;
|
|
|
|
extern std::mt19937_64 mt;
|
|
|
|
|
2020-10-06 15:46:36 -07:00
|
|
|
// Random integer
|
2017-08-28 14:51:22 -07:00
|
|
|
uint64_t integer();
|
2018-08-22 17:19:18 -07:00
|
|
|
uint64_t integer(const uint64_t &min, const uint64_t &max); // inclusive
|
2017-08-19 14:27:51 -06:00
|
|
|
|
2020-10-07 02:59:30 -07:00
|
|
|
// Random vector
|
|
|
|
template<class T> T vector() = delete;
|
|
|
|
template<> u128x1 vector();
|
|
|
|
template<> u256x1 vector();
|
|
|
|
template<> u512x1 vector();
|
|
|
|
|
2017-08-28 14:51:22 -07:00
|
|
|
// Random character from dictionary
|
|
|
|
char character(const std::string &dict = dict::alnum);
|
2017-08-19 14:27:51 -06:00
|
|
|
|
2020-10-06 15:46:36 -07:00
|
|
|
// Random string from dictionary, fills buffer
|
|
|
|
string_view string(const mutable_buffer &out, const std::string &dict);
|
2020-10-06 16:01:53 -07:00
|
|
|
|
|
|
|
// Random fill of buffer
|
|
|
|
const_buffer fill(const mutable_buffer &out);
|
2017-08-28 14:51:22 -07:00
|
|
|
}
|
2017-08-19 14:27:51 -06:00
|
|
|
|
2020-10-06 15:46:36 -07:00
|
|
|
/// Random character from dictionary
|
2017-08-19 14:27:51 -06:00
|
|
|
inline char
|
|
|
|
ircd::rand::character(const std::string &dict)
|
|
|
|
{
|
|
|
|
assert(!dict.empty());
|
2020-10-06 15:46:36 -07:00
|
|
|
const auto pos
|
|
|
|
{
|
|
|
|
integer(0, dict.size() - 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
return dict.at(pos);
|
2017-08-19 14:27:51 -06:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:46:36 -07:00
|
|
|
/// Random integer in range (inclusive)
|
2017-08-19 14:27:51 -06:00
|
|
|
inline uint64_t
|
|
|
|
ircd::rand::integer(const uint64_t &min,
|
|
|
|
const uint64_t &max)
|
|
|
|
{
|
2020-10-06 15:46:36 -07:00
|
|
|
std::uniform_int_distribution<uint64_t> dist
|
|
|
|
{
|
|
|
|
min, max
|
|
|
|
};
|
|
|
|
|
2017-08-19 14:27:51 -06:00
|
|
|
return dist(mt);
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:46:36 -07:00
|
|
|
/// Random 64-bits
|
2017-08-19 14:27:51 -06:00
|
|
|
inline uint64_t
|
|
|
|
ircd::rand::integer()
|
|
|
|
{
|
|
|
|
return mt();
|
|
|
|
}
|