Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #include "util/GridSearch.hpp"
4 : : #include <random>
5 : : #include <type_traits>
6 : : #include <utility>
7 : :
8 : :
9 : : namespace m {
10 : :
11 : : /** Utilities to help generate data, e.g. for experiment setups. */
12 : : namespace datagen {
13 : :
14 : : /** Generate `count` many distinct numbers of type `T`, chosen uniformly at random from the range [`min`, `max`]. Uses
15 : : * generator `g` for randomness in the data. */
16 : : template<typename T, typename Generator = std::mt19937_64>
17 : 6 : std::vector<T> generate_uniform_distinct_numbers(const T min, const T max, const std::size_t count,
18 : : Generator &&g = Generator())
19 : : {
20 : : static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type");
21 : 6 : M_insist(min <= max);
22 : 6 : M_insist(is_range_wide_enough<T>(min, max, count), "range [min, max] does not have count distinct values");
23 : :
24 [ + - # # : 6 : if (count == 1) return std::vector<T>(1, min);
+ - # # +
- # # + -
# # + - #
# + - #
# ]
25 : 6 : std::vector<T> values = gs::LinearSpace<T>(min, max, count-1).sequence();
26 : :
27 [ + - + - : 6 : std::shuffle(values.begin(), values.end(), std::forward<Generator>(g));
+ - + - +
- + - ]
28 : :
29 : 6 : return values;
30 [ + - + - : 12 : }
+ - + - +
- + - ]
31 : :
32 : : }
33 : :
34 : : }
|