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