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 <chrono>
11 #include <memory>
12 #include <random>
13 #include <thread>
14 
15 #include "util/logging.h"
16 
17 namespace colmap {
18 
19 extern thread_local std::unique_ptr<std::mt19937> PRNG;
20 
21 static int kDefaultPRNGSeed = 0;
22 
23 // Initialize the PRNG with the given seed.
24 //
25 // @param seed The seed for the PRNG. If the seed is -1, the current time
26 // is used as the seed.
27 void SetPRNGSeed(unsigned seed = kDefaultPRNGSeed);
28 
29 // Generate uniformly distributed random integer number.
30 //
31 // This implementation is unbiased and thread-safe in contrast to `rand()`.
32 template <typename T>
33 T RandomInteger(const T min, const T max);
34 
35 // Generate uniformly distributed random real number.
36 //
37 // This implementation is unbiased and thread-safe in contrast to `rand()`.
38 template <typename T>
39 T RandomReal(const T min, const T max);
40 
41 // Generate Gaussian distributed random real number.
42 //
43 // This implementation is unbiased and thread-safe in contrast to `rand()`.
44 template <typename T>
45 T RandomGaussian(const T mean, const T stddev);
46 
47 // Fisher-Yates shuffling.
48 //
49 // Note that the vector may not contain more values than UINT32_MAX. This
50 // restriction comes from the fact that the 32-bit version of the
51 // Mersenne Twister PRNG is significantly faster.
52 //
53 // @param elems Vector of elements to shuffle.
54 // @param num_to_shuffle Optional parameter, specifying the number of first
55 // N elements in the vector to shuffle.
56 template <typename T>
57 void Shuffle(const uint32_t num_to_shuffle, std::vector<T>* elems);
58 
60 // Implementation
62 
63 template <typename T>
64 T RandomInteger(const T min, const T max) {
65  if (PRNG == nullptr) {
66  SetPRNGSeed();
67  }
68 
69  std::uniform_int_distribution<T> distribution(min, max);
70 
71  return distribution(*PRNG);
72 }
73 
74 template <typename T>
75 T RandomReal(const T min, const T max) {
76  if (PRNG == nullptr) {
77  SetPRNGSeed();
78  }
79 
80  std::uniform_real_distribution<T> distribution(min, max);
81 
82  return distribution(*PRNG);
83 }
84 
85 template <typename T>
86 T RandomGaussian(const T mean, const T stddev) {
87  if (PRNG == nullptr) {
88  SetPRNGSeed();
89  }
90 
91  std::normal_distribution<T> distribution(mean, stddev);
92  return distribution(*PRNG);
93 }
94 
95 template <typename T>
96 void Shuffle(const uint32_t num_to_shuffle, std::vector<T>* elems) {
97  CHECK_LE(num_to_shuffle, elems->size());
98  const uint32_t last_idx = static_cast<uint32_t>(elems->size() - 1);
99  for (uint32_t i = 0; i < num_to_shuffle; ++i) {
100  const auto j = RandomInteger<uint32_t>(i, last_idx);
101  std::swap((*elems)[i], (*elems)[j]);
102  }
103 }
104 
105 } // namespace colmap
static int kDefaultPRNGSeed
Definition: random.h:21
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
thread_local std::unique_ptr< std::mt19937 > PRNG
Definition: random.cc:38
T RandomInteger(const T min, const T max)
Definition: random.h:64
T RandomGaussian(const T mean, const T stddev)
Definition: random.h:86
T RandomReal(const T min, const T max)
Definition: random.h:75
void Shuffle(const uint32_t num_to_shuffle, std::vector< T > *elems)
Definition: random.h:96
void swap(cloudViewer::core::SmallVectorImpl< T > &LHS, cloudViewer::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370
double stddev(std::vector< double > const &func)
Definition: qAutoSeg.cpp:86