random.h 4.2 KB

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