ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
random_numbers.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/definitions/algebra.hpp"
13#include "detray/definitions/detail/qualifiers.hpp"
14
15// System include(s)
16#include <array>
17#include <limits>
18#include <random>
19
20namespace detray::detail {
21
23template <concepts::scalar scalar_t,
24 typename distribution_t = std::uniform_real_distribution<scalar_t>,
25 typename engine_t = std::mt19937_64>
28 using distribution_type = distribution_t;
30 using engine_type = engine_t;
32 using seed_type = typename engine_t::result_type;
33
35 std::seed_seq m_seeds;
37 engine_t m_engine;
38
40 DETRAY_HOST
43
46 DETRAY_HOST
48
51 DETRAY_HOST
52 explicit random_numbers(const std::vector<seed_type>& s)
53 : m_seeds{s.begin(), s.end()}, m_engine{m_seeds} {}
54
57 DETRAY_HOST
59 : m_engine(std::move(other.m_engine)) {}
60
64 DETRAY_HOST auto operator()(const darray<scalar_t, 2> range = {
65 -std::numeric_limits<scalar_t>::max(),
66 std::numeric_limits<scalar_t>::max()}) {
67 const scalar_t min{range[0]};
68 const scalar_t max{range[1]};
69 assert(min <= max);
70
71 // Uniform
72 if constexpr (std::is_same_v<distribution_t,
73 std::uniform_real_distribution<scalar_t>>) {
74 return distribution_t(min, max)(m_engine);
75
76 // Normal
77 } else if constexpr (std::is_same_v<distribution_t,
78 std::normal_distribution<scalar_t>>) {
79 scalar_t mu{min + 0.5f * (max - min)};
80 return distribution_t(mu, 0.5f / 3.0f * (max - min))(m_engine);
81 }
82 }
83
88 DETRAY_HOST auto normal(const scalar_t mean, const scalar_t stddev) {
89 return (stddev == static_cast<scalar_t>(0))
90 ? mean
91 : std::normal_distribution<scalar_t>(mean, stddev)(m_engine);
92 }
93
96 DETRAY_HOST std::uint8_t coin_toss() {
97 return std::uniform_int_distribution<std::uint8_t>(0u, 1u)(m_engine);
98 }
99
102 static constexpr seed_type default_seed() { return engine_t::default_seed; }
103};
104
105} // namespace detray::detail
constexpr double min
Minute - 60 seconds.
Definition Units.hpp:132
constexpr decltype(auto) max(T &&vec)
Definition vc_soa_math.hpp:151
std::array< value_t, kDIM > darray
Fixed-size array container.
Definition containers.hpp:30
random_numbers()
Default seed.
Definition random_numbers.hpp:41
random_numbers(seed_type s)
Different seed s for every instance.
Definition random_numbers.hpp:47
std::uint8_t coin_toss()
50:50 coin toss
Definition random_numbers.hpp:96
std::seed_seq m_seeds
Seed sequence.
Definition random_numbers.hpp:35
static constexpr seed_type default_seed()
Get the default seed of the engine.
Definition random_numbers.hpp:102
engine_t m_engine
Random engine.
Definition random_numbers.hpp:37
typename engine_t::result_type seed_type
Random seed type.
Definition random_numbers.hpp:32
random_numbers(const std::vector< seed_type > &s)
More entropy in seeds from collection s.
Definition random_numbers.hpp:52
auto operator()(const darray< scalar_t, 2 > range={ -std::numeric_limits< scalar_t >::max(), std::numeric_limits< scalar_t >::max()})
Generate random numbers in a given range.
Definition random_numbers.hpp:64
random_numbers(random_numbers &&other) noexcept
Move constructor.
Definition random_numbers.hpp:58
engine_t engine_type
Random engine type.
Definition random_numbers.hpp:30
distribution_t distribution_type
Random distribution type.
Definition random_numbers.hpp:28
auto normal(const scalar_t mean, const scalar_t stddev)
Explicit normal distribution around a mean and stddev.
Definition random_numbers.hpp:88