Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
random.h
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 OPENCV_FLANN_RANDOM_H 00032 #define OPENCV_FLANN_RANDOM_H 00033 00034 #include <algorithm> 00035 #include <cstdlib> 00036 #include <vector> 00037 00038 #include "general.h" 00039 00040 namespace cvflann 00041 { 00042 00043 /** 00044 * Seeds the random number generator 00045 * @param seed Random seed 00046 */ 00047 inline void seed_random(unsigned int seed) 00048 { 00049 srand(seed); 00050 } 00051 00052 /* 00053 * Generates a random double value. 00054 */ 00055 /** 00056 * Generates a random double value. 00057 * @param high Upper limit 00058 * @param low Lower limit 00059 * @return Random double value 00060 */ 00061 inline double rand_double(double high = 1.0, double low = 0) 00062 { 00063 return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0))); 00064 } 00065 00066 /** 00067 * Generates a random integer value. 00068 * @param high Upper limit 00069 * @param low Lower limit 00070 * @return Random integer value 00071 */ 00072 inline int rand_int(int high = RAND_MAX, int low = 0) 00073 { 00074 return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0))); 00075 } 00076 00077 /** 00078 * Random number generator that returns a distinct number from 00079 * the [0,n) interval each time. 00080 */ 00081 class UniqueRandom 00082 { 00083 std::vector<int> vals_; 00084 int size_; 00085 int counter_; 00086 00087 public: 00088 /** 00089 * Constructor. 00090 * @param n Size of the interval from which to generate 00091 * @return 00092 */ 00093 UniqueRandom(int n) 00094 { 00095 init(n); 00096 } 00097 00098 /** 00099 * Initializes the number generator. 00100 * @param n the size of the interval from which to generate random numbers. 00101 */ 00102 void init(int n) 00103 { 00104 // create and initialize an array of size n 00105 vals_.resize(n); 00106 size_ = n; 00107 for (int i = 0; i < size_; ++i) vals_[i] = i; 00108 00109 // shuffle the elements in the array 00110 std::random_shuffle(vals_.begin(), vals_.end()); 00111 00112 counter_ = 0; 00113 } 00114 00115 /** 00116 * Return a distinct random integer in greater or equal to 0 and less 00117 * than 'n' on each call. It should be called maximum 'n' times. 00118 * Returns: a random integer 00119 */ 00120 int next() 00121 { 00122 if (counter_ == size_) { 00123 return -1; 00124 } 00125 else { 00126 return vals_[counter_++]; 00127 } 00128 } 00129 }; 00130 00131 } 00132 00133 #endif //OPENCV_FLANN_RANDOM_H 00134
Generated on Tue Jul 12 2022 16:42:39 by
1.7.2