scran_test_utils
Test utilities for libscran projects
Loading...
Searching...
No Matches
simulate_vector.hpp
Go to the documentation of this file.
1#ifndef SCRAN_TESTS_SIMULATE_VECTOR_HPP
2#define SCRAN_TESTS_SIMULATE_VECTOR_HPP
3
4#include <random>
5#include <vector>
6#include <cstdint>
7#include <type_traits>
8#include <cstddef>
9
15namespace scran_tests {
16
22template<typename Type_ = double>
28 if constexpr(std::is_unsigned<Type_>::value) {
29 lower = 0;
30 }
31 }
39 double density = 1;
40
45 Type_ lower = -10;
46
50 Type_ upper = 10;
51
55 std::uint64_t seed = 1234567890;
56};
57
69template<typename Type_ = double>
70std::vector<Type_> simulate_vector(std::size_t length, const SimulationParameters<Type_>& params) {
71 std::mt19937_64 rng(params.seed);
72
73 typename std::conditional<
74 std::is_floating_point<Type_>::value,
75 std::uniform_real_distribution<Type_>,
76 std::uniform_int_distribution<Type_>
77 >::type unif(params.lower, params.upper);
78
79 std::vector<Type_> values(length);
80
81 if (params.density == 1) {
82 for (auto& v : values) {
83 v = unif(rng);
84 }
85 } else {
86 std::uniform_real_distribution<double> nonzero(0.0, 1.0);
87 for (auto& v : values) {
88 if (nonzero(rng) < params.density) {
89 v = unif(rng);
90 }
91 }
92 }
93
94 return values;
95}
96
97}
98
99#endif
Test utilites for libscran.
Definition compare_almost_equal.hpp:12
std::vector< Type_ > simulate_vector(std::size_t length, const SimulationParameters< Type_ > &params)
Definition simulate_vector.hpp:70
Parameters for simulate_vector().
Definition simulate_vector.hpp:23
std::uint64_t seed
Definition simulate_vector.hpp:55
double density
Definition simulate_vector.hpp:39
Type_ upper
Definition simulate_vector.hpp:50
Type_ lower
Definition simulate_vector.hpp:45