random.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef OPENCV_FLANN_RANDOM_H
  31. #define OPENCV_FLANN_RANDOM_H
  32. //! @cond IGNORED
  33. #include <algorithm>
  34. #include <cstdlib>
  35. #include <vector>
  36. #include "general.h"
  37. namespace cvflann
  38. {
  39. inline int rand()
  40. {
  41. #ifndef OPENCV_FLANN_USE_STD_RAND
  42. # if INT_MAX == RAND_MAX
  43. int v = cv::theRNG().next() & INT_MAX;
  44. # else
  45. int v = cv::theRNG().uniform(0, RAND_MAX + 1);
  46. # endif
  47. #else
  48. int v = std::rand();
  49. #endif // OPENCV_FLANN_USE_STD_RAND
  50. return v;
  51. }
  52. /**
  53. * Seeds the random number generator
  54. * @param seed Random seed
  55. */
  56. inline void seed_random(unsigned int seed)
  57. {
  58. #ifndef OPENCV_FLANN_USE_STD_RAND
  59. cv::theRNG() = cv::RNG(seed);
  60. #else
  61. std::srand(seed);
  62. #endif
  63. }
  64. /*
  65. * Generates a random double value.
  66. */
  67. /**
  68. * Generates a random double value.
  69. * @param high Upper limit
  70. * @param low Lower limit
  71. * @return Random double value
  72. */
  73. inline double rand_double(double high = 1.0, double low = 0)
  74. {
  75. return low + ((high-low) * (rand() / (RAND_MAX + 1.0)));
  76. }
  77. /**
  78. * Generates a random integer value.
  79. * @param high Upper limit
  80. * @param low Lower limit
  81. * @return Random integer value
  82. */
  83. inline int rand_int(int high = RAND_MAX, int low = 0)
  84. {
  85. return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0)));
  86. }
  87. /**
  88. * Random number generator that returns a distinct number from
  89. * the [0,n) interval each time.
  90. */
  91. class UniqueRandom
  92. {
  93. std::vector<int> vals_;
  94. int size_;
  95. int counter_;
  96. public:
  97. /**
  98. * Constructor.
  99. * @param n Size of the interval from which to generate
  100. * @return
  101. */
  102. UniqueRandom(int n)
  103. {
  104. init(n);
  105. }
  106. /**
  107. * Initializes the number generator.
  108. * @param n the size of the interval from which to generate random numbers.
  109. */
  110. void init(int n)
  111. {
  112. // create and initialize an array of size n
  113. vals_.resize(n);
  114. size_ = n;
  115. for (int i = 0; i < size_; ++i) vals_[i] = i;
  116. // shuffle the elements in the array
  117. #ifndef OPENCV_FLANN_USE_STD_RAND
  118. cv::randShuffle(vals_);
  119. #else
  120. std::random_shuffle(vals_.begin(), vals_.end());
  121. #endif
  122. counter_ = 0;
  123. }
  124. /**
  125. * Return a distinct random integer in greater or equal to 0 and less
  126. * than 'n' on each call. It should be called maximum 'n' times.
  127. * Returns: a random integer
  128. */
  129. int next()
  130. {
  131. if (counter_ == size_) {
  132. return -1;
  133. }
  134. else {
  135. return vals_[counter_++];
  136. }
  137. }
  138. };
  139. }
  140. //! @endcond
  141. #endif //OPENCV_FLANN_RANDOM_H