MorphoGraphX  2.0-1-227
Random.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef RANDOM_HPP
12 #define RANDOM_HPP
13 
19 #include <Config.hpp>
20 
21 #include <Vector.hpp>
22 
23 #ifdef WIN32
24 static long int random() { return rand(); }
25 
26 static void srandom(unsigned int seed) { return srand(seed); }
27 #endif
28 
29 namespace mgx
30 {
37  mgx_EXPORT unsigned int sran_time();
38 
42  mgx_EXPORT void sran(unsigned int seed);
43 
47  mgx_EXPORT double ran(double M);
51  mgx_EXPORT long double ran(long double M);
55  mgx_EXPORT float ran(float M);
56 
60  template <typename T> double ran(T M) {
61  return ran(double(M));
62  }
63 
67  template <size_t dim, typename T> Vector<dim, T> ran(const Vector<dim, T>& V)
68  {
69  Vector<dim, T> result;
70  for(size_t i = 0; i < dim; ++i)
71  result[i] = ran(V[i]);
72  return result;
73  }
74 
81  mgx_EXPORT double gaussRan(double mean, double sigma);
82 
91  template <size_t dim> Vector<dim, double> gaussRan(const Vector<dim, double>& mean, const Vector<dim, double>& sigma)
92  {
93  Vector<dim, double> result;
94  for(size_t i = 0; i < dim; ++i)
95  result[i] = gaussRan(mean[i], sigma[i]);
96  return result;
97  }
98 
102  mgx_EXPORT long int ranInt();
103 
107  template <typename T> T ranInt()
108  {
109  long int i = ranInt();
110  return (T)i;
111  }
112 
116  template <size_t dim, typename T> Vector<dim, T> ranInt()
117  {
118  Vector<dim, long int> result;
119  for(size_t i = 0; i < dim; ++i)
120  result[i] = ranInt<T>();
121  return result;
122  }
123 
127  template <size_t dim> Vector<dim, long int> ranInt() {
128  return ranInt<dim, long int>();
129  }
130 
134  mgx_EXPORT long int ranInt(long int n);
135 
140  template <size_t dim, typename T> Vector<dim, T> ranInt(const Vector<dim, T>& n)
141  {
142  Vector<dim, T> result;
143  for(size_t i = 0; i < dim; ++i)
144  result[i] = ranInt<T>(n[i]);
145  return result;
146  }
147 }
148 #endif
Vector.hpp
mgx::gaussRan
mgx_EXPORT double gaussRan(double mean, double sigma)
Generate a random number with gaussian distribution.
n
#define n
Definition: Eigenvalues.hpp:36
mgx::sran
mgx_EXPORT void sran(unsigned int seed)
Initialize the random number generator.
mgx::ran
mgx_EXPORT double ran(double M)
Generate a random number uniformly distributed between 0 and M.
mgx::ranInt
mgx_EXPORT long int ranInt()
Returns a random number between 0 and RAND_MAX.
Definition: Random.hpp:107
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Vector
Namespace containing all the utility classes.
Definition: Vector.hpp:48
mgx::sran_time
mgx_EXPORT unsigned int sran_time()
Initialize the random number with the current time of the day (in microsecond)