You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file random.h
  2. ** @brief Random number generator (@ref random)
  3. ** @author Andrea Vedaldi
  4. ** @see @ref random
  5. **/
  6. /*
  7. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. #ifndef VL_RANDOM_H
  13. #define VL_RANDOM_H
  14. #include "host.h"
  15. /** @brief Random numbber generator state */
  16. typedef struct _VlRand {
  17. vl_uint32 mt [624] ;
  18. vl_uint32 mti ;
  19. } VlRand ;
  20. /** @name Setting and reading the state
  21. **
  22. ** @{ */
  23. VL_EXPORT void vl_rand_init (VlRand * self) ;
  24. VL_EXPORT void vl_rand_seed (VlRand * self, vl_uint32 s) ;
  25. VL_EXPORT void vl_rand_seed_by_array (VlRand * self,
  26. vl_uint32 const key [],
  27. vl_size keySize) ;
  28. /** @} */
  29. /** @name Generate random numbers
  30. **
  31. ** @{ */
  32. VL_INLINE vl_uint64 vl_rand_uint64 (VlRand * self) ;
  33. VL_INLINE vl_int64 vl_rand_int63 (VlRand * self) ;
  34. VL_EXPORT vl_uint32 vl_rand_uint32 (VlRand * self) ;
  35. VL_INLINE vl_int32 vl_rand_int31 (VlRand * self) ;
  36. VL_INLINE double vl_rand_real1 (VlRand * self) ;
  37. VL_INLINE double vl_rand_real2 (VlRand * self) ;
  38. VL_INLINE double vl_rand_real3 (VlRand * self) ;
  39. VL_INLINE double vl_rand_res53 (VlRand * self) ;
  40. VL_INLINE vl_uindex vl_rand_uindex (VlRand * self, vl_uindex range) ;
  41. /** @} */
  42. VL_EXPORT void vl_rand_permute_indexes (VlRand * self, vl_index* array, vl_size size) ;
  43. /* ---------------------------------------------------------------- */
  44. /** @brief Generate a random index in a given range
  45. ** @param self random number generator.
  46. ** @param range range.
  47. ** @return an index sampled uniformly at random in the interval [0, @c range - 1]
  48. **
  49. ** @remark Currently, this function uses a simple algorithm that
  50. ** may yield slightly biased samples if @c range is not a power of
  51. ** two.
  52. **/
  53. VL_INLINE vl_uindex
  54. vl_rand_uindex (VlRand * self, vl_uindex range)
  55. {
  56. if (range <= 0xffffffff) {
  57. /* 32-bit version */
  58. return (vl_rand_uint32 (self) % (vl_uint32)range) ;
  59. } else {
  60. /* 64-bit version */
  61. return (vl_rand_uint64 (self) % range) ;
  62. }
  63. }
  64. /** @brief Generate a random UINT64
  65. ** @param self random number generator.
  66. ** @return a random number in [0, 0xffffffffffffffff].
  67. **/
  68. VL_INLINE vl_uint64
  69. vl_rand_uint64 (VlRand * self)
  70. {
  71. vl_uint64 a = vl_rand_uint32 (self) ;
  72. vl_uint64 b = vl_rand_uint32 (self) ;
  73. return (a << 32) | b ;
  74. }
  75. /** @brief Generate a random INT63
  76. ** @param self random number generator.
  77. ** @return a random number in [0, 0x7fffffffffffffff].
  78. **/
  79. VL_INLINE vl_int64
  80. vl_rand_int63 (VlRand * self)
  81. {
  82. return (vl_int64)(vl_rand_uint64 (self) >> 1) ;
  83. }
  84. /** @brief Generate a random INT31
  85. ** @param self random number generator.
  86. ** @return a random number in [0, 0x7fffffff].
  87. **/
  88. VL_INLINE vl_int32
  89. vl_rand_int31 (VlRand * self)
  90. {
  91. return (vl_int32)(vl_rand_uint32 (self) >> 1) ;
  92. }
  93. /** @brief Generate a random number in [0,1]
  94. ** @param self random number generator.
  95. ** @return a random number.
  96. **/
  97. VL_INLINE double
  98. vl_rand_real1 (VlRand * self)
  99. {
  100. return vl_rand_uint32(self)*(1.0/4294967295.0);
  101. /* divided by 2^32-1 */
  102. }
  103. /** @brief Generate a random number in [0,1)
  104. ** @param self random number generator.
  105. ** @return a random number.
  106. **/
  107. VL_INLINE double
  108. vl_rand_real2 (VlRand * self)
  109. {
  110. return vl_rand_uint32(self)*(1.0/4294967296.0);
  111. /* divided by 2^32 */
  112. }
  113. /** @brief Generate a random number in (0,1)
  114. ** @param self random number generator.
  115. ** @return a random number.
  116. **/
  117. VL_INLINE double
  118. vl_rand_real3 (VlRand * self)
  119. {
  120. return (((double)vl_rand_uint32(self)) + 0.5)*(1.0/4294967296.0);
  121. /* divided by 2^32 */
  122. }
  123. /** @brief Generate a random number in [0,1) with 53-bit resolution
  124. ** @param self random number generator.
  125. ** @return a random number.
  126. **/
  127. VL_INLINE double
  128. vl_rand_res53 (VlRand * self)
  129. {
  130. vl_uint32
  131. a = vl_rand_uint32(self) >> 5,
  132. b = vl_rand_uint32(self) >> 6 ;
  133. return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0) ;
  134. }
  135. /* VL_RANDOM_H */
  136. #endif