OpenCV 4.5.3(日本語機械翻訳)
random.h
1 /***********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6 *
7 * THE BSD LICENSE
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *************************************************************************/
30
31 #ifndef OPENCV_FLANN_RANDOM_H
32 #define OPENCV_FLANN_RANDOM_H
33
35
36 #include <algorithm>
37 #include <cstdlib>
38 #include <vector>
39
40 namespace cvflann
41{
42
43 inline int rand()
44{
45 #ifndef OPENCV_FLANN_USE_STD_RAND
46 # if INT_MAX == RAND_MAX
47 int v = cv::theRNG().next() & INT_MAX;
48 # else
49 int v = cv::theRNG().uniform(0, RAND_MAX + 1);
50 # endif
51 #else
52 int v = std::rand();
53 #endif // OPENCV_FLANN_USE_STD_RAND
54 return v;
55}
56
61 inline void seed_random(unsigned int seed)
62{
63 #ifndef OPENCV_FLANN_USE_STD_RAND
64 cv::theRNG() = cv::RNG(seed);
65 #else
66 std::srand(seed);
67 #endif
68}
69
70 /*
71 * Generates a random double value.
72 */
79 inline double rand_double(double high = 1.0, double low = 0)
80{
81 return low + ((high-low) * (rand() / (RAND_MAX + 1.0)));
82}
83
90 inline int rand_int(int high = RAND_MAX, int low = 0)
91{
92 return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0)));
93}
94
99 class UniqueRandom
100{
101 std::vector<int> vals_;
102 int size_;
103 int counter_;
104
105 public:
111 UniqueRandom(int n)
112 {
113 init(n);
114 }
115
120 void init(int n)
121 {
122 // create and initialize an array of size n
123 vals_.resize(n);
124 size_ = n;
125 for (int i = 0; i < size_; ++i) vals_[i] = i;
126
127 // shuffle the elements in the array
128 #ifndef OPENCV_FLANN_USE_STD_RAND
129 cv::randShuffle(vals_);
130 #else
131 std::random_shuffle(vals_.begin(), vals_.end());
132 #endif
133
134 counter_ = 0;
135 }
136
142 int next()
143 {
144 if (counter_ == size_) {
145 return -1;
146 }
147 else {
148 return vals_[counter_++];
149 }
150 }
151};
152
153}
154
156
157 #endif //OPENCV_FLANN_RANDOM_H
Random Number Generator
Definition: core.hpp:2783
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
unsigned next()
CV_EXPORTS RNG & theRNG()
Returns the default random number generator.
CV_EXPORTS_W void randShuffle(InputOutputArray dst, double iterFactor=1., RNG *rng=0)
Shuffles the array elements randomly.