MorphoGraphX
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() {
25  return rand();
26 }
27 
28 static void srandom(unsigned int seed) {
29  return srand(seed);
30 }
31 #endif
32 
33 namespace mgx {
34 namespace util {
41 mgx_EXPORT unsigned int sran_time();
42 
46 mgx_EXPORT void sran(unsigned int seed);
47 
51 mgx_EXPORT double ran(double M);
55 mgx_EXPORT long double ran(long double M);
59 mgx_EXPORT float ran(float M);
60 
64 template <typename T> double ran(T M) {
65  return ran(double(M));
66 }
67 
71 template <size_t dim, typename T> Vector<dim, T> ran(const Vector<dim, T>& V)
72 {
73  Vector<dim, T> result;
74  for(size_t i = 0; i < dim; ++i)
75  result[i] = ran(V[i]);
76  return result;
77 }
78 
85 mgx_EXPORT double gaussRan(double mean, double sigma);
86 
95 template <size_t dim> Vector<dim, double> gaussRan(const Vector<dim, double>& mean, const Vector<dim, double>& sigma)
96 {
97  Vector<dim, double> result;
98  for(size_t i = 0; i < dim; ++i)
99  result[i] = gaussRan(mean[i], sigma[i]);
100  return result;
101 }
102 
106 mgx_EXPORT long int ranInt();
107 
111 template <typename T> T ranInt()
112 {
113  long int i = ranInt();
114  return (T)i;
115 }
116 
120 template <size_t dim, typename T> Vector<dim, T> ranInt()
121 {
122  Vector<dim, long int> result;
123  for(size_t i = 0; i < dim; ++i)
124  result[i] = ranInt<T>();
125  return result;
126 }
127 
131 template <size_t dim> Vector<dim, long int> ranInt() {
132  return ranInt<dim, long int>();
133 }
134 
138 mgx_EXPORT long int ranInt(long int n);
139 
144 template <size_t dim, typename T> Vector<dim, T> ranInt(const Vector<dim, T>& n)
145 {
146  Vector<dim, T> result;
147  for(size_t i = 0; i < dim; ++i)
148  result[i] = ranInt<T>(n[i]);
149  return result;
150 }
151 } // namespace util
152 } // namespace mgx
153 #endif
mgx_EXPORT double ran(double M)
Generate a random number uniformely distributed between 0 and M.
mgx_EXPORT void sran(unsigned int seed)
Initialize the random number generator.
mgx_EXPORT unsigned int sran_time()
Initialize the random number with the current time of the day (in microsecond)
mgx_EXPORT long int ranInt()
Returns a random number between 0 and RAND_MAX.
Definition: Random.hpp:111
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Namespace containing all the utility classes.
Definition: Vector.hpp:47
mgx_EXPORT double gaussRan(double mean, double sigma)
Generate a random number with gaussian distribution.
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...