ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Random.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Logging.h>
11 
12 #include <algorithm>
13 #include <mutex>
14 #include <random>
15 #include <vector>
16 
17 namespace cloudViewer {
18 namespace utility {
19 namespace random {
20 
22 void Seed(const int seed);
23 
37 std::mt19937* GetEngine();
38 
41 std::mutex* GetMutex();
42 
46 uint32_t RandUint32();
47 
65 template <typename T>
67 public:
73  UniformIntGenerator(const T low, const T high) : distribution_(low, high) {
74  if (low < 0) {
75  utility::LogError("low must be > 0, but got {}.", low);
76  }
77  if (low > high) {
79  "low must be <= high, but got low={} and high={}.", low,
80  high);
81  }
82  }
83 
85  T operator()() {
86  std::lock_guard<std::mutex> lock(*GetMutex());
87  return distribution_(*GetEngine());
88  }
89 
90 protected:
91  std::uniform_int_distribution<T> distribution_;
92 };
93 
111 template <typename T>
113 public:
118  UniformRealGenerator(const T low = 0.0, const T high = 1.0)
119  : distribution_(low, high) {
120  if (low >= high) {
121  utility::LogError("low must be < high, but got low={} and high={}.",
122  low, high);
123  }
124  }
125 
128  std::lock_guard<std::mutex> lock(*GetMutex());
129  return distribution_(*GetEngine());
130  }
131 
132 protected:
133  std::uniform_real_distribution<T> distribution_;
134 };
135 
153 template <typename T>
155 public:
160  NormalGenerator(const T mean = 0.0, const T stddev = 1.0)
161  : distribution_(mean, stddev) {
162  if (stddev <= 0) {
163  utility::LogError("stddev must be > 0, but got {}.", stddev);
164  }
165  }
166 
169  std::lock_guard<std::mutex> lock(*GetMutex());
170  return distribution_(*GetEngine());
171  }
172 
173 protected:
174  std::normal_distribution<T> distribution_;
175 };
176 
196 template <typename T>
198 public:
205  template <typename InputIt>
206  DiscreteGenerator(InputIt first, InputIt last)
207  : distribution_(first, last) {
208  if (first > last) {
209  utility::LogError("first must be <= last.");
210  }
211  }
212 
215  std::lock_guard<std::mutex> lock(*GetMutex());
216  return distribution_(*GetEngine());
217  }
218 
219 protected:
220  std::discrete_distribution<T> distribution_;
221 };
222 
223 } // namespace random
224 } // namespace utility
225 } // namespace cloudViewer
T operator()()
Call this to generate a discretely distributed integer value.
Definition: Random.h:214
DiscreteGenerator(InputIt first, InputIt last)
Definition: Random.h:206
std::discrete_distribution< T > distribution_
Definition: Random.h:220
T operator()()
Call this to generate a normally distributed floating point value.
Definition: Random.h:168
std::normal_distribution< T > distribution_
Definition: Random.h:174
NormalGenerator(const T mean=0.0, const T stddev=1.0)
Definition: Random.h:160
std::uniform_int_distribution< T > distribution_
Definition: Random.h:91
UniformIntGenerator(const T low, const T high)
Definition: Random.h:73
T operator()()
Call this to generate a uniformly distributed integer.
Definition: Random.h:85
T operator()()
Call this to generate a uniformly distributed floating point value.
Definition: Random.h:127
std::uniform_real_distribution< T > distribution_
Definition: Random.h:133
UniformRealGenerator(const T low=0.0, const T high=1.0)
Definition: Random.h:118
#define LogError(...)
Definition: Logging.h:60
std::mt19937 * GetEngine()
Definition: Random.cpp:55
void Seed(const int seed)
Set CloudViewer global random seed.
Definition: Random.cpp:53
std::mutex * GetMutex()
Definition: Random.cpp:57
Generic file read and write utility for python interface.
double stddev(std::vector< double > const &func)
Definition: qAutoSeg.cpp:86