Disable crashing tests, my previous checkin to mark them flaky did not help.
[chromium-blink-merge.git] / base / rand_util.cc
blob4a455f1bd9e2a29d3aae27c6b6d9911c7ea401ec
1 // Copyright (c) 2010 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.
5 #include "base/rand_util.h"
7 #include <math.h>
9 #include <limits>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
14 namespace base {
16 int RandInt(int min, int max) {
17 DCHECK(min <= max);
19 uint64 range = static_cast<uint64>(max) - min + 1;
20 int result = min + static_cast<int>(base::RandGenerator(range));
21 DCHECK(result >= min && result <= max);
22 return result;
25 double RandDouble() {
26 // We try to get maximum precision by masking out as many bits as will fit
27 // in the target type's mantissa, and raising it to an appropriate power to
28 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
29 // is expected to accommodate 53 bits.
31 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
32 static const int kBits = std::numeric_limits<double>::digits;
33 uint64 random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1);
34 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
35 DCHECK(result >= 0.0 && result < 1.0);
36 return result;
39 uint64 RandGenerator(uint64 max) {
40 DCHECK_GT(max, 0ULL);
41 return base::RandUint64() % max;
44 } // namespace base