summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-30 21:07:05 +0000
committerisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-30 21:07:05 +0000
commita74dcaeaa91acee7dde40b9680eb6e0e30df11fd (patch)
tree41d17386b9ed6f7b4266d3332860c954b64358e9 /base
parentbf898dad1b57c6fd436bc8d8c8ef143e3d11755d (diff)
downloadchromium_src-a74dcaeaa91acee7dde40b9680eb6e0e30df11fd.zip
chromium_src-a74dcaeaa91acee7dde40b9680eb6e0e30df11fd.tar.gz
chromium_src-a74dcaeaa91acee7dde40b9680eb6e0e30df11fd.tar.bz2
Add RandomNumberGenerator adapter to base/rand_util.h
BUG=46679 TEST=none (yet...) Review URL: http://codereview.chromium.org/3053050 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57904 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/rand_util.cc12
-rw-r--r--base/rand_util.h9
-rw-r--r--base/rand_util_unittest.cc8
3 files changed, 24 insertions, 5 deletions
diff --git a/base/rand_util.cc b/base/rand_util.cc
index 8480c82..0576c94 100644
--- a/base/rand_util.cc
+++ b/base/rand_util.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -16,9 +16,8 @@ namespace base {
int RandInt(int min, int max) {
DCHECK(min <= max);
- uint64 range = static_cast<int64>(max) - min + 1;
- uint64 number = base::RandUint64();
- int result = min + static_cast<int>(number % range);
+ uint64 range = static_cast<uint64>(max) - min + 1;
+ int result = min + static_cast<int>(base::RandGenerator(range));
DCHECK(result >= min && result <= max);
return result;
}
@@ -37,4 +36,9 @@ double RandDouble() {
return result;
}
+uint64 RandGenerator(uint64 max) {
+ DCHECK(max > 0);
+ return base::RandUint64() % max;
+}
+
} // namespace base
diff --git a/base/rand_util.h b/base/rand_util.h
index 451a7d1..699fc7f 100644
--- a/base/rand_util.h
+++ b/base/rand_util.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -16,6 +16,13 @@ uint64 RandUint64();
// Returns a random number between min and max (inclusive). Thread-safe.
int RandInt(int min, int max);
+// Returns a random number in range [0, max). Thread-safe.
+//
+// Note that this can be used as an adapter for std::random_shuffle():
+// Given a pre-populated |std::vector<int> myvector|, shuffle it as
+// std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator);
+uint64 RandGenerator(uint64 max);
+
// Returns a random double in range [0, 1). Thread-safe.
double RandDouble();
diff --git a/base/rand_util_unittest.cc b/base/rand_util_unittest.cc
index f56c0ec..cbc338a 100644
--- a/base/rand_util_unittest.cc
+++ b/base/rand_util_unittest.cc
@@ -27,3 +27,11 @@ TEST(RandUtilTest, RandDouble) {
EXPECT_GT(1.0, number);
EXPECT_LE(0.0, number);
}
+
+// Make sure that it is still appropriate to use RandGenerator in conjunction
+// with std::random_shuffle().
+TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
+ EXPECT_EQ(base::RandGenerator(1), 0U);
+ EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
+ std::numeric_limits<int64>::max());
+}