exact_uniform_int.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // Exact uniform integers using rejection sampling
  13. #ifndef TENSORFLOW_LIB_RANDOM_EXACT_UNIFORM_H_
  14. #define TENSORFLOW_LIB_RANDOM_EXACT_UNIFORM_H_
  15. #include <type_traits>
  16. namespace tensorflow {
  17. namespace random {
  18. template <typename UintType, typename RandomBits>
  19. UintType ExactUniformInt(const UintType n, const RandomBits& random) {
  20. static_assert(std::is_unsigned<UintType>::value, "UintType must be an unsigned int");
  21. static_assert(std::is_same<UintType, decltype(random())>::value,
  22. "random() should return UintType");
  23. if (n == 0) {
  24. // Consume a value anyway
  25. // TODO(irving): Assert n != 0, since this case makes no sense.
  26. return random() * n;
  27. } else if (0 == (n & (n - 1))) {
  28. // N is a power of two, so just mask off the lower bits.
  29. return random() & (n - 1);
  30. } else {
  31. // Reject all numbers that skew the distribution towards 0.
  32. // random's output is uniform in the half-open interval [0, 2^{bits}).
  33. // For any interval [m,n), the number of elements in it is n-m.
  34. const UintType range = ~static_cast<UintType>(0);
  35. const UintType rem = (range % n) + 1;
  36. UintType rnd;
  37. // rem = ((2^bits-1) \bmod n) + 1
  38. // 1 <= rem <= n
  39. // NB: rem == n is impossible, since n is not a power of 2 (from
  40. // earlier check).
  41. do {
  42. rnd = random(); // rnd uniform over [0, 2^{bits})
  43. } while (rnd < rem); // reject [0, rem)
  44. // rnd is uniform over [rem, 2^{bits})
  45. //
  46. // The number of elements in the half-open interval is
  47. //
  48. // 2^{bits} - rem = 2^{bits} - ((2^{bits}-1) \bmod n) - 1
  49. // = 2^{bits}-1 - ((2^{bits}-1) \bmod n)
  50. // = n \cdot \lfloor (2^{bits}-1)/n \rfloor
  51. //
  52. // therefore n evenly divides the number of integers in the
  53. // interval.
  54. //
  55. // The function v \rightarrow v % n takes values from [bias,
  56. // 2^{bits}) to [0, n). Each integer in the range interval [0, n)
  57. // will have exactly \lfloor (2^{bits}-1)/n \rfloor preimages from
  58. // the domain interval.
  59. //
  60. // Therefore, v % n is uniform over [0, n). QED.
  61. return rnd % n;
  62. }
  63. }
  64. } // namespace random
  65. } // namespace tensorflow
  66. #endif // TENSORFLOW_LIB_RANDOM_EXACT_UNIFORM_H_