EstervQrCode 1.1.1
Library for qr code manipulation
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:
110  UniqueRandom(int n)
111  {
112  init(n);
113  }
114 
119  void init(int n)
120  {
121  // create and initialize an array of size n
122  vals_.resize(n);
123  size_ = n;
124  for (int i = 0; i < size_; ++i) vals_[i] = i;
125 
126  // shuffle the elements in the array
127 #ifndef OPENCV_FLANN_USE_STD_RAND
128  cv::randShuffle(vals_);
129 #else
130  std::random_shuffle(vals_.begin(), vals_.end());
131 #endif
132 
133  counter_ = 0;
134  }
135 
141  int next()
142  {
143  if (counter_ == size_) {
144  return -1;
145  }
146  else {
147  return vals_[counter_++];
148  }
149  }
150 };
151 
152 }
153 
155 
156 #endif //OPENCV_FLANN_RANDOM_H
T begin(T... args)
Random Number Generator.
Definition: core.hpp:2889
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
unsigned next()
T end(T... args)
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.
Definition: flann.hpp:60
T next(T... args)
T rand(T... args)
T random_shuffle(T... args)
T resize(T... args)
T srand(T... args)