random.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #ifndef MATH_UTIL_RANDOM_H_
31 #define MATH_UTIL_RANDOM_H_
32 
33 #include <vector>
34 
35 #include <boost/random/mersenne_twister.hpp>
36 #include <boost/random/uniform_int.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <boost/thread/mutex.hpp>
39 
40 #ifdef BOOST_1_46
41  #include <boost/nondet_random.hpp>
42 #else
43  #include <boost/random/random_device.hpp>
44 #endif
45 
46 namespace swri_math_util
47 {
48  #ifdef BOOST_1_46
49  namespace boost_random = boost;
50  #else
51  namespace boost_random = boost::random;
52  #endif
53 
55  {
56  public:
57  explicit RandomGenerator(int32_t seed = -1);
58 
60  int32_t min,
61  int32_t max,
62  int32_t count,
63  std::vector<int32_t>& sample);
64 
65  private:
66  boost_random::random_device seed_;
67  boost_random::mt19937 rng_;
68  boost::mutex mutex_;
69  };
71 
88  template <class RNG>
90  RNG& rng,
91  int32_t min,
92  int32_t max,
93  int32_t count,
94  std::vector<int32_t>& sample)
95  {
96  sample.clear();
97  if (count < 0)
98  {
99  return;
100  }
101 
102  if (min > max)
103  {
104  int32_t tmp = min;
105  min = max;
106  max = tmp;
107  }
108 
109  int32_t range = (max - min) + 1;
110  if (count > range)
111  {
112  count = range;
113  }
114 
115  sample.resize(count);
116 
117  boost::uniform_int<> dist(min, max);
118  for (int32_t i = 0; i < count; i++)
119  {
120  bool has_sample = false;
121  while (!has_sample)
122  {
123  sample[i] = dist(rng);
124  int32_t j;
125  for (j = 0; j < i; j++)
126  {
127  if (sample[i] == sample[j]) break;
128  }
129  has_sample = j == i;
130  }
131  }
132  }
133 }
134 
135 #endif // MATH_UTIL_RANDOM_H_
RandomGenerator(int32_t seed=-1)
Definition: random.cpp:34
void GetUniformRandomSample(int32_t min, int32_t max, int32_t count, std::vector< int32_t > &sample)
Definition: random.cpp:39
boost::shared_ptr< RandomGenerator > RandomGeneratorPtr
Definition: random.h:70
boost_random::random_device seed_
Definition: random.h:66
boost_random::mt19937 rng_
Definition: random.h:67


swri_math_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:05:41