fw4spl
Generator.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTOOLS_RANDOM_GENERATOR_HPP__
8 #define __FWTOOLS_RANDOM_GENERATOR_HPP__
9 
10 #include <fwCore/base.hpp>
11 
12 #include <boost/concept_check.hpp>
13 #include <boost/mpl/if.hpp>
14 #include <boost/random/mersenne_twister.hpp>
15 #include <boost/random/uniform_int.hpp>
16 #include <boost/random/uniform_real.hpp>
17 #include <boost/random/variate_generator.hpp>
18 
19 #include <algorithm>
20 #include <ctime>
21 #include <type_traits>
22 
23 namespace fwTools
24 {
25 namespace random
26 {
27 
37 template <typename T>
38 T getValue(T min, T max, std::uint32_t seedVal = std::time(NULL))
39 {
40  SLM_ASSERT("Wrong min/max value", min <= max);
41  typedef typename ::boost::mpl::if_<
42  std::is_floating_point<T>,
43  ::boost::uniform_real<>,
44  ::boost::uniform_int<>
45  >::type DistroType;
46 
47  ::boost::mt19937 seed(seedVal);
48  DistroType dist(min, max);
49  ::boost::variate_generator< ::boost::mt19937&, DistroType > random(seed, dist);
50  return random();
51 }
52 
63 template <typename T, typename CONTAINER>
64 void fillContainer(T min, T max, CONTAINER& randContainer, std::uint32_t seedVal = std::time(NULL))
65 {
66  SLM_ASSERT("Wrong min/max value", min <= max);
67  SLM_ASSERT("Container type not same as T", (std::is_same< T, typename CONTAINER::value_type>::value) );
68  typedef typename ::boost::mpl::if_<
69  std::is_floating_point<T>,
70  ::boost::uniform_real<>,
71  ::boost::uniform_int<>
72  >::type DistroType;
73 
74  ::boost::mt19937 seed(seedVal);
75  DistroType dist(min, max);
76  ::boost::variate_generator< ::boost::mt19937&, DistroType > random(seed, dist);
77  std::generate(randContainer.begin(), randContainer.end(), random);
78 }
79 
80 } // namespace random
81 } // namespace fwTools
82 
83 #endif //__FWTOOLS_RANDOM_GENERATOR_HPP__
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308