1 #ifndef PROBA_DISTRIBUTIONS_TPP_
2 #define PROBA_DISTRIBUTIONS_TPP_
6 #include <cstdlib> // std::rand
12 //---------------------------------------------------------------------------
14 template < typename T >
15 UniformRandomDistribution<T>::UniformRandomDistribution(T min, T max)
18 { assert(min <= max); }
20 //---------------------------------------------------------------------------
22 template < typename T >
23 T UniformRandomDistribution<T>::operator()()
25 if (std::numeric_limits<T>::is_integer)
27 // Epsilon correspond to the safety margin we want on the side of the
28 // continuous uniform distribution, to be sure that when rounding we
29 // never get min-1 or max+1
30 // NB: with epsilon == 0 , I once obtained max+1 !!
31 // TODO: is this much better / efficient than two tests (on min and max)?
32 const double eps = 32 * std::numeric_limits<double>::epsilon();
33 T res = castValue<double, T>(std::rand() / double(RAND_MAX) * (m_max - m_min + 1 - 2*eps) + m_min - 0.5 + eps);
40 // TODO: is there the same issue here as above?
41 T res = castValue<double, T>(std::rand() / double(RAND_MAX) * (m_max - m_min) + m_min);
48 //---------------------------------------------------------------------------
50 template < typename T >
53 UniformRandomDistribution<T> u(min, max);
57 //---------------------------------------------------------------------------
61 #endif /*PROBA_DISTRIBUTIONS_TPP_*/