PointSampler library (C++)
Loading...
Searching...
No Matches
rejection_sampling.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
8
9namespace ps
10{
11
63template <typename T, size_t N, typename DensityFn>
64std::vector<Point<T, N>> rejection_sampling(
65 size_t count,
66 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 std::array<std::uniform_real_distribution<T>, N> coord_dists;
73 for (size_t i = 0; i < N; ++i)
74 coord_dists[i] = std::uniform_real_distribution<T>(axis_ranges[i].first,
76
77 std::vector<Point<T, N>> candidates;
78 candidates.reserve(count * 2); // overgenerate
79
80 while (candidates.size() < count * 2)
81 {
83 for (size_t i = 0; i < N; ++i)
84 p[i] = coord_dists[i](gen);
85
86 candidates.push_back(p);
87 }
88
90}
91
92} // 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
std::vector< Point< T, N > > rejection_sampling(size_t count, const std::array< std::pair< T, T >, N > &axis_ranges, DensityFn density_fn, std::optional< unsigned int > seed=std::nullopt)
Generates random points using rejection sampling based on a user-defined density function.
Definition rejection_sampling.hpp:64
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39