LCOV - code coverage report
Current view: top level - ipc/chromium/src/base - rand_util.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 7 12 58.3 %
Date: 2017-07-14 16:53:18 Functions: 1 2 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : // Copyright (c) 2008 The Chromium Authors. All rights reserved.
       4             : // Use of this source code is governed by a BSD-style license that can be
       5             : // found in the LICENSE file.
       6             : 
       7             : #include "base/rand_util.h"
       8             : 
       9             : #include <math.h>
      10             : 
      11             : #include <limits>
      12             : 
      13             : #include "base/basictypes.h"
      14             : #include "base/logging.h"
      15             : 
      16             : namespace base {
      17             : 
      18          15 : int RandInt(int min, int max) {
      19          15 :   DCHECK(min <= max);
      20             : 
      21          15 :   uint64_t range = static_cast<int64_t>(max) - min + 1;
      22          15 :   uint64_t number = base::RandUint64();
      23          15 :   int result = min + static_cast<int>(number % range);
      24          15 :   DCHECK(result >= min && result <= max);
      25          15 :   return result;
      26             : }
      27             : 
      28           0 : double RandDouble() {
      29             :   // We try to get maximum precision by masking out as many bits as will fit
      30             :   // in the target type's mantissa, and raising it to an appropriate power to
      31             :   // produce output in the range [0, 1).  For IEEE 754 doubles, the mantissa
      32             :   // is expected to accommodate 53 bits.
      33             : 
      34             :   COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
      35             :   static const int kBits = std::numeric_limits<double>::digits;
      36           0 :   uint64_t random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1);
      37           0 :   double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
      38           0 :   DCHECK(result >= 0.0 && result < 1.0);
      39           0 :   return result;
      40             : }
      41             : 
      42             : }  // namespace base

Generated by: LCOV version 1.13