ORE Studio 0.0.4
Loading...
Searching...
No Matches
generation_engine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_UTILITY_GENERATION_GENERATION_ENGINE_HPP
21#define ORES_UTILITY_GENERATION_GENERATION_ENGINE_HPP
22
23#include <array>
24#include <chrono>
25#include <cstdint>
26#include <random>
27#include <stdexcept>
28#include <string>
29#include <vector>
30#include <boost/uuid/uuid.hpp>
31
32namespace ores::utility::generation {
33
41class generation_engine final {
42public:
46 explicit generation_engine(std::uint64_t seed);
47
52
56 std::uint64_t seed() const { return seed_; }
57
61 int random_int(int min, int max);
62
66 bool random_bool(double probability = 0.5);
67
71 template<typename T>
72 const T& pick(const std::vector<T>& items) {
73 if (items.empty()) {
74 throw std::out_of_range("Cannot pick from an empty vector.");
75 }
76 std::uniform_int_distribution<std::size_t> dist(0, items.size() - 1);
77 return items[dist(engine_)];
78 }
79
83 template<typename T, std::size_t N>
84 const T& pick(const std::array<T, N>& items) {
85 static_assert(N > 0, "Cannot pick from an empty array.");
86 std::uniform_int_distribution<std::size_t> dist(0, N - 1);
87 return items[dist(engine_)];
88 }
89
93 boost::uuids::uuid generate_uuid();
94
98 std::chrono::system_clock::time_point past_timepoint(int years_back = 3);
99
103 std::string alphanumeric(std::size_t length);
104
105private:
106 std::uint64_t seed_;
107 std::mt19937_64 engine_;
108};
109
110}
111
112#endif
Random generation engine with seed-controlled repeatability.
Definition generation_engine.hpp:41
generation_engine()
Constructs an engine with a random seed.
Definition generation_engine.cpp:27
std::string alphanumeric(std::size_t length)
Generates an alphanumeric string of specified length.
Definition generation_engine.cpp:81
const T & pick(const std::array< T, N > &items)
Picks a random element from an array.
Definition generation_engine.hpp:84
bool random_bool(double probability=0.5)
Generates a random boolean with specified probability of true.
Definition generation_engine.cpp:35
std::uint64_t seed() const
Returns the seed used for this engine.
Definition generation_engine.hpp:56
std::chrono::system_clock::time_point past_timepoint(int years_back=3)
Generates a random timestamp within the past N years.
Definition generation_engine.cpp:75
const T & pick(const std::vector< T > &items)
Picks a random element from a vector.
Definition generation_engine.hpp:72
int random_int(int min, int max)
Generates a random integer in [min, max].
Definition generation_engine.cpp:30
boost::uuids::uuid generate_uuid()
Generates a UUID v7 based on the engine's random state.
Definition generation_engine.cpp:40