Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JMath/JRandom.hh
Go to the documentation of this file.
1#ifndef __JMATH__JRANDOM__
2#define __JMATH__JRANDOM__
3
4#include <limits>
5
6#include "TRandom3.h"
7
8#include "JMath/JLimits.hh"
9
10/**
11 * \file
12 *
13 * Definition of random value generator.
14 * \author mdejong
15 */
16namespace JMATH {}
17namespace JPP { using namespace JMATH; }
18
19namespace JMATH {
20
21
22 /**
23 * Template definition of random value generator.
24 */
25 template<class T, bool is_specialized = JLimits<T>::is_specialized>
26 struct JRandom;
27
28
29 /**
30 * Template spacialisation of JMATH::JRandom for numerical values.
31 */
32 template<class T>
33 struct JRandom<T, true> {
34 /**
35 * Get uniformly distributed random value between numerical limits.
36 *
37 * \return random value
38 */
39 static inline T getRandom()
40 {
41 return gRandom->Uniform((Double_t) JLimits<T>::min(), (Double_t) JLimits<T>::max());
42 }
43
44
45 /**
46 * Get uniformly distributed random value between given limits.
47 *
48 * \param min minimal value
49 * \param max maximal value
50 * \return random value
51 */
52 static inline T getRandom(const T min,
53 const T max)
54 {
55 return (T) gRandom->Uniform((Double_t) min, (Double_t) max);
56 }
57 };
58
59
60 /**
61 * Template specialisation for data type <tt>double</tt> because TRandom has 32 bits.
62 */
63 template<>
65 {
66 return gRandom->Uniform((Double_t) JLimits<float>::min(), (Double_t) JLimits<float>::max());
67 }
68
69
70 /**
71 * Template spacialisation of JMATH::JRandom for non-numerical data types.
72 *
73 * The template argument <tt>T</tt> should have the default constructor and
74 * the following method should be implemented for this data type.
75 * <pre>
76 * void %randomize(T* object);
77 * </pre>
78 */
79 template<class T>
80 struct JRandom<T, false> {
81 /**
82 * Get random value.
83 *
84 * \return random value
85 */
86 static inline const T& getRandom()
87 {
88 new (&value) T();
89
90 randomize(&value);
91
92 return value;
93 }
94
95 private:
96 static T value;
97 };
98
99
100 /**
101 * Definition of value.
102 */
103 template<class T>
105
106
107 /**
108 * Get random value.
109 *
110 * \return random value
111 */
112 template<class T>
113 inline T getRandom()
114 {
115 return JRandom<T>::getRandom();
116 }
117
118
119 /**
120 * Get uniformly distributed random value between given limits.
121 *
122 * \param min minimal value
123 * \param max maximal value
124 * \return random value
125 */
126 template<class T>
127 inline T getRandom(const T min,
128 const T max)
129 {
130 return JRandom<T, true>::getRandom(min, max);
131 }
132
133
134 /**
135 * Get uniformly distributed random value between given limits.
136 *
137 * \param min minimal value
138 * \param max maximal value
139 * \param precision precision
140 * \return random value
141 */
142 template<class T>
143 inline T getRandom(const T min,
144 const T max,
145 const T precision)
146 {
147 return JRandom<T, true>::getRandom(min, max);
148 }
149
150
151 /**
152 * Template specialisation for data type <tt>double</tt>.
153 */
154 template<>
155 inline double getRandom(const double min,
156 const double max,
157 const double precision)
158 {
159 double value = getRandom<double>(min, max);
160
161 if (precision != 0.0) {
162
163 const long long int ip = (long long int) (1.0 / precision);
164
165 value = ((double) ((long long int) (value * ip))) / ip;
166 }
167
168 return value;
169 }
170}
171
172#endif
Definition of minimum and maximum values for any class.
Auxiliary classes and methods for mathematical operations.
Definition JEigen3D.hh:88
void randomize(JMatrix1D *p)
Randomize matrix.
T getRandom()
Get random value.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for minimum and maximum values for any class.
Definition JLimits.hh:21
static const T & getRandom()
Get random value.
static T getRandom(const T min, const T max)
Get uniformly distributed random value between given limits.
static T getRandom()
Get uniformly distributed random value between numerical limits.
Template definition of random value generator.
double getRandom()
Template specialisation for data type double because TRandom has 32 bits.