@@ -18,14 +18,13 @@ class tpzrand
18
18
mt ().seed (seq);
19
19
}
20
20
21
- /* Generates a random number in the half-open interval [min, max)
22
- @param min
23
- @param max
24
- @returns result
25
- */
21
+ // Generates a random number in the half-open interval [min, max]
22
+ // @param min
23
+ // @param max
24
+ // @returns result
26
25
template <typename T>
27
26
static inline typename std::enable_if<std::is_integral<T>::value, T>::type
28
- GetRandomNumber (T min, T max)
27
+ GetRandomNumber (T min, T max)
29
28
{
30
29
if (min == max - 1 || max == min)
31
30
{
@@ -37,7 +36,7 @@ class tpzrand
37
36
38
37
template <typename T>
39
38
static inline typename std::enable_if<std::is_floating_point<T>::value, T>::type
40
- GetRandomNumber (T min, T max)
39
+ GetRandomNumber (T min, T max)
41
40
{
42
41
if (min == max)
43
42
{
@@ -47,14 +46,39 @@ class tpzrand
47
46
return dist (mt ());
48
47
}
49
48
50
- /* Generates a random number in the half-open interval [0, max)
51
- @param min
52
- @param max
53
- @returns result
54
- */
49
+ // Generates a random number in the half-open interval [0, max)
50
+ // @param min
51
+ // @param max
52
+ // @returns result
55
53
template <typename T>
56
54
static inline T GetRandomNumber (T max)
57
55
{
58
56
return GetRandomNumber<T>(0 , max);
59
57
}
58
+
59
+ // Gets a random element from the given stl-like container (container must have members: at() and size()).
60
+ // @param container
61
+ // @returns result
62
+ template <typename T> static inline typename T::value_type GetRandomElement (T* container)
63
+ {
64
+ // NOTE: the specialisation for integral types uses: dist(min, max - 1), so no need to offset container->size()
65
+ return container->at (GetRandomNumber<std::size_t >(0U , container->size ()));
66
+ }
67
+
68
+ // Gets a random element from the given stl-like container (container must have members: at() and size()).
69
+ // @param container
70
+ // @returns result
71
+ template <typename T> static inline typename T::value_type GetRandomElement (T& container)
72
+ {
73
+ return GetRandomElement (&container);
74
+ }
75
+
76
+ // Gets a random element from the given initializer_list.
77
+ // @param initializer_list
78
+ // @returns result
79
+ template <typename T> static inline T GetRandomElement (std::initializer_list<T> list)
80
+ {
81
+ std::vector<T> container (list);
82
+ return GetRandomElement (container);
83
+ }
60
84
};
0 commit comments