Random.h
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Vincent Rouvreau
4 *
5 * Copyright (C) 2024 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
11#ifndef GUDHI_RANDOM__H_
12#define GUDHI_RANDOM__H_
13
14#include <random> // for std::random_device, std::mt19937, std::uniform_real_distribution
15#include <vector>
16#include <algorithm> // for std::generate
17#include <cstddef> // for std::size_t
18
19namespace Gudhi {
20 class Random {
21 public:
22 Random() : gen_(rd_()) {}
23 Random(std::uint_fast32_t seed) : gen_(seed) {}
24
25 template <typename Type>
26 Type get(const Type& min = 0, const Type& max = 1) {
27 if constexpr (std::is_floating_point_v<Type>) {
28 std::uniform_real_distribution<Type> dis(min, max);
29 return dis(gen_);
30 } else if constexpr (std::is_integral_v<Type>) {
31 std::uniform_int_distribution<Type> dis(min, max);
32 return dis(gen_);
33 }
34 }
35
36 template <typename Type>
37 std::vector<Type> get_range(std::size_t nbr, const Type& min = 0, const Type& max = 1) {
38 std::vector<Type> result(nbr);
39 if constexpr (std::is_floating_point_v<Type>) {
40 std::uniform_real_distribution<Type> dis(min, max);
41 std::generate(result.begin(), result.end(), [&]() { return dis(gen_); });
42 } else if constexpr (std::is_integral_v<Type>) {
43 std::uniform_int_distribution<Type> dis(min, max);
44 std::generate(result.begin(), result.end(), [&]() { return dis(gen_); });
45 }
46 return result;
47 }
48
49 private:
50 std::mt19937_64 gen_;
51 inline static std::random_device rd_;
52 };
53} // namespace Gudhi
54
55#endif // GUDHI_RANDOM__H_
constexpr auto & get(Gudhi::persistence_matrix::Persistence_interval< Dimension, Event_value > &i) noexcept
Partial specialization of get for Gudhi::persistence_matrix::Persistence_interval.
Definition: persistence_interval.h:199
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14