Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/rand_util.h"
      6 
      7 #include <limits.h>
      8 #include <math.h>
      9 #include <stdint.h>
     10 
     11 #include <algorithm>
     12 #include <limits>
     13 
     14 #include "base/logging.h"
     15 #include "base/strings/string_util.h"
     16 
     17 namespace base {
     18 
     19 uint64_t RandUint64() {
     20   uint64_t number;
     21   RandBytes(&number, sizeof(number));
     22   return number;
     23 }
     24 
     25 int RandInt(int min, int max) {
     26   DCHECK_LE(min, max);
     27 
     28   uint64_t range = static_cast<uint64_t>(max) - min + 1;
     29   // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range)
     30   // is at most UINT_MAX.  Hence it's safe to cast it from uint64_t to int64_t.
     31   int result =
     32       static_cast<int>(min + static_cast<int64_t>(base::RandGenerator(range)));
     33   DCHECK_GE(result, min);
     34   DCHECK_LE(result, max);
     35   return result;
     36 }
     37 
     38 double RandDouble() {
     39   return BitsToOpenEndedUnitInterval(base::RandUint64());
     40 }
     41 
     42 double BitsToOpenEndedUnitInterval(uint64_t bits) {
     43   // We try to get maximum precision by masking out as many bits as will fit
     44   // in the target type's mantissa, and raising it to an appropriate power to
     45   // produce output in the range [0, 1).  For IEEE 754 doubles, the mantissa
     46   // is expected to accommodate 53 bits.
     47 
     48   static_assert(std::numeric_limits<double>::radix == 2,
     49                 "otherwise use scalbn");
     50   static const int kBits = std::numeric_limits<double>::digits;
     51   uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1);
     52   double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
     53   DCHECK_GE(result, 0.0);
     54   DCHECK_LT(result, 1.0);
     55   return result;
     56 }
     57 
     58 uint64_t RandGenerator(uint64_t range) {
     59   DCHECK_GT(range, 0u);
     60   // We must discard random results above this number, as they would
     61   // make the random generator non-uniform (consider e.g. if
     62   // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
     63   // as likely as a result of 3 or 4).
     64   uint64_t max_acceptable_value =
     65       (std::numeric_limits<uint64_t>::max() / range) * range - 1;
     66 
     67   uint64_t value;
     68   do {
     69     value = base::RandUint64();
     70   } while (value > max_acceptable_value);
     71 
     72   return value % range;
     73 }
     74 
     75 std::string RandBytesAsString(size_t length) {
     76   DCHECK_GT(length, 0u);
     77   std::string result;
     78   RandBytes(WriteInto(&result, length + 1), length);
     79   return result;
     80 }
     81 
     82 }  // namespace base
     83