random.h
Go to the documentation of this file.
00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * THE BSD LICENSE
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *************************************************************************/
00030 
00031 #ifndef RTABMAP_FLANN_RANDOM_H
00032 #define RTABMAP_FLANN_RANDOM_H
00033 
00034 #include <algorithm>
00035 #include <cstdlib>
00036 #include <cstddef>
00037 #include <vector>
00038 
00039 #include "rtflann/general.h"
00040 
00041 namespace rtflann
00042 {
00043 
00048 inline void seed_random(unsigned int seed)
00049 {
00050     srand(seed);
00051 }
00052 
00053 /*
00054  * Generates a random double value.
00055  */
00062 inline double rand_double(double high = 1.0, double low = 0)
00063 {
00064     return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0)));
00065 }
00066 
00073 inline int rand_int(int high = RAND_MAX, int low = 0)
00074 {
00075     return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0)));
00076 }
00077 
00078 
00079 class RandomGenerator
00080 {
00081 public:
00082     ptrdiff_t operator() (ptrdiff_t i) { return rand_int(i); }
00083 };
00084 
00085 
00090 class UniqueRandom
00091 {
00092     std::vector<int> vals_;
00093     int size_;
00094     int counter_;
00095 
00096 public:
00102     UniqueRandom(int n)
00103     {
00104         init(n);
00105     }
00106 
00111     void init(int n)
00112     {
00113         static RandomGenerator generator;
00114         // create and initialize an array of size n
00115         vals_.resize(n);
00116         size_ = n;
00117         for (int i = 0; i < size_; ++i) vals_[i] = i;
00118 
00119         // shuffle the elements in the array
00120         std::random_shuffle(vals_.begin(), vals_.end(), generator);
00121 
00122         counter_ = 0;
00123     }
00124 
00130     int next()
00131     {
00132         if (counter_ == size_) {
00133             return -1;
00134         }
00135         else {
00136             return vals_[counter_++];
00137         }
00138     }
00139 };
00140 
00141 }
00142 
00143 #endif //FLANN_RANDOM_H
00144 
00145 


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:17