#ifndef __JMATH__JRANDOM__ #define __JMATH__JRANDOM__ #include #include "TRandom3.h" #include "JMath/JLimits.hh" /** * \file * * Definition of random value generator. * \author mdejong */ namespace JMATH {} namespace JPP { using namespace JMATH; } namespace JMATH { /** * Template definition of random value generator. */ template::is_specialized> struct JRandom; /** * Template spacialisation of JMATH::JRandom for numerical values. */ template struct JRandom { /** * Get uniformly distributed random value between numerical limits. * * \return random value */ static inline T getRandom() { return gRandom->Uniform((Double_t) JLimits::min(), (Double_t) JLimits::max()); } /** * Get uniformly distributed random value between given limits. * * \param min minimal value * \param max maximal value * \return random value */ static inline T getRandom(const T min, const T max) { return (T) gRandom->Uniform((Double_t) min, (Double_t) max); } }; /** * Template specialisation for data type double because TRandom has 32 bits. */ template<> inline double JRandom::getRandom() { return gRandom->Uniform((Double_t) JLimits::min(), (Double_t) JLimits::max()); } /** * Template spacialisation of JMATH::JRandom for non-numerical data types. * * The template argument T should have the default constructor and * the following method should be implemented for this data type. *
   *   void %randomize(T* object);
   * 
*/ template struct JRandom { /** * Get random value. * * \return random value */ static inline const T& getRandom() { new (&value) T(); randomize(&value); return value; } private: static T value; }; /** * Definition of value. */ template T JRandom::value; /** * Get random value. * * \return random value */ template inline T getRandom() { return JRandom::getRandom(); } /** * Get uniformly distributed random value between given limits. * * \param min minimal value * \param max maximal value * \return random value */ template inline T getRandom(const T min, const T max) { return JRandom::getRandom(min, max); } /** * Get uniformly distributed random value between given limits. * * \param min minimal value * \param max maximal value * \param precision precision * \return random value */ template inline T getRandom(const T min, const T max, const T precision) { return JRandom::getRandom(min, max); } /** * Template specialisation for data type double. */ template<> inline double getRandom(const double min, const double max, const double precision) { double value = getRandom(min, max); if (precision != 0.0) { const long long int ip = (long long int) (1.0 / precision); value = ((double) ((long long int) (value * ip))) / ip; } return value; } } #endif