random.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #ifndef MATH_UTIL_RANDOM_H_
00031 #define MATH_UTIL_RANDOM_H_
00032 
00033 #include <vector>
00034 
00035 #include <boost/random/mersenne_twister.hpp>
00036 #include <boost/random/uniform_int.hpp>
00037 #include <boost/shared_ptr.hpp>
00038 #include <boost/thread/mutex.hpp>
00039 
00040 #ifdef BOOST_1_46
00041   #include <boost/nondet_random.hpp>
00042 #else
00043   #include <boost/random/random_device.hpp>
00044 #endif
00045 
00046 namespace swri_math_util
00047 {
00048   #ifdef BOOST_1_46
00049   namespace boost_random = boost;
00050   #else
00051   namespace boost_random = boost::random;
00052   #endif
00053 
00054   class RandomGenerator
00055   {
00056     public:
00057       explicit RandomGenerator(int32_t seed = -1);
00058       
00059       void GetUniformRandomSample(
00060         int32_t min, 
00061         int32_t max, 
00062         int32_t count,
00063         std::vector<int32_t>& sample);
00064       
00065     private:
00066       boost_random::random_device seed_;
00067       boost_random::mt19937 rng_;
00068       boost::mutex mutex_;
00069   };
00070   typedef boost::shared_ptr<RandomGenerator> RandomGeneratorPtr;
00071 
00088   template <class RNG>
00089   void GetUniformRandomSample(
00090     RNG& rng,
00091     int32_t min, 
00092     int32_t max, 
00093     int32_t count,
00094     std::vector<int32_t>& sample)
00095   {
00096     sample.clear();
00097     if (count < 0)
00098     {
00099       return;
00100     }
00101     
00102     if (min > max)
00103     {
00104       int32_t tmp = min;
00105       min = max;
00106       max = tmp;
00107     }
00108     
00109     int32_t range = (max - min) + 1;
00110     if (count > range)
00111     {
00112       count = range;
00113     }
00114 
00115     sample.resize(count);
00116       
00117     boost::uniform_int<> dist(min, max);
00118     for (int32_t i = 0; i < count; i++)
00119     {
00120       bool has_sample = false;
00121       while (!has_sample)
00122       {
00123         sample[i] = dist(rng);
00124         int32_t j;
00125         for (j = 0; j < i; j++)
00126         {
00127           if (sample[i] == sample[j]) break;
00128         }
00129         has_sample = j == i;
00130       }
00131     }
00132   }
00133 }
00134 
00135 #endif  // MATH_UTIL_RANDOM_H_


swri_math_util
Author(s): Marc Alban
autogenerated on Tue Oct 3 2017 03:19:18