Direct Graphical Models  v.1.7.0
random.h
1 // Random numbers generation
2 // Written by Sergey G. Kosov in 2013 for Project X
3 // Simplified (C++11) by Sergey G. Kosov in 2016 for Project X
4 #pragma once
5 
6 #include "types.h"
7 #include <random>
8 
9 namespace DirectGraphicalModels
10 {
11  // ================================ Random Namespace ==============================
17  namespace random {
28  template <typename T>
29  inline T u(T min, T max)
30  {
31  static thread_local std::mt19937 generator(static_cast<unsigned int>(clock() + std::hash<std::thread::id>()(std::this_thread::get_id())));
32  std::uniform_int_distribution<T> distribution(min, max);
33  return distribution(generator);
34  }
45  template <typename T>
46  inline T U(T min = 0, T max = 1)
47  {
48  static thread_local std::mt19937 generator(static_cast<unsigned int>(clock() + std::hash<std::thread::id>()(std::this_thread::get_id())));
49  std::uniform_real_distribution<T> distribution(min, max);
50  return distribution(generator);
51  }
62  template <typename T>
63  inline T N(T mu = 0, T sigma = 1)
64  {
65  static thread_local std::mt19937 generator(static_cast<unsigned int>(clock() + std::hash<std::thread::id>()(std::this_thread::get_id())));
66  std::normal_distribution<T> distribution(mu, sigma);
67  return distribution(generator);
68  }
69 
70 
79  inline Mat U(cv::Size size, int type, double min = 0, double max = 1)
80  {
81  static thread_local RNG rng(static_cast<unsigned int>(clock() + std::hash<std::thread::id>()(std::this_thread::get_id())));
82  Mat res(size, type);
83  rng.fill(res, RNG::UNIFORM, min, max);
84  return res;
85  }
94  inline Mat N(cv::Size size, int type, double mu = 0, double sigma = 1)
95  {
96  static thread_local RNG rng(static_cast<unsigned int>(clock() + std::hash<std::thread::id>()(std::this_thread::get_id())));
97  Mat res(size, type);
98  rng.fill(res, RNG::NORMAL, mu, sigma);
99  return res;
100  }
101 
102  }
103 }
104 
T U(T min=0, T max=1)
Returns a floating-point random number with uniform distribution.
Definition: random.h:46
T u(T min, T max)
Returns an integer random number with uniform distribution.
Definition: random.h:29
T N(T mu=0, T sigma=1)
Returns a floating-point random number with normal distribution.
Definition: random.h:63