PointSampler library (C++)
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4#pragma once
5#include <optional>
6#include <random>
7
9
10namespace ps
11{
12
65template <typename T, size_t N>
66std::vector<Point<T, N>> random(size_t count,
67 const std::array<std::pair<T, T>, N> &axis_ranges,
68 std::optional<unsigned int> seed = std::nullopt)
69{
70 std::mt19937 gen(seed ? *seed : std::random_device{}());
71
72 // Create N distributions, one per dimension
73 std::array<std::uniform_real_distribution<T>, N> dists;
74 for (size_t i = 0; i < N; ++i)
75 {
76 const auto &[min_val, max_val] = axis_ranges[i];
77 if (min_val > max_val)
78 throw std::invalid_argument("Invalid axis range: min > max");
79 dists[i] = std::uniform_real_distribution<T>(min_val, max_val);
80 }
81
82 std::vector<Point<T, N>> points;
83 points.reserve(count);
84
85 for (size_t i = 0; i < count; ++i)
86 {
88 for (size_t j = 0; j < N; ++j)
89 p[j] = dists[j](gen);
90 points.push_back(p);
91 }
92
93 return points;
94}
95
96} // namespace ps
Definition dbscan_clustering.hpp:11
std::vector< Point< T, N > > random(size_t count, const std::array< std::pair< T, T >, N > &axis_ranges, std::optional< unsigned int > seed=std::nullopt)
Generates a specified number of uniformly distributed random points in N-dimensional space.
Definition random.hpp:66
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39