diff options
-rw-r--r-- | base/rand_util.cc | 12 | ||||
-rw-r--r-- | base/rand_util.h | 9 | ||||
-rw-r--r-- | base/rand_util_unittest.cc | 8 | ||||
-rw-r--r-- | chrome/browser/views/first_run_search_engine_view.cc | 7 | ||||
-rw-r--r-- | net/websockets/websocket_handshake.cc | 2 |
5 files changed, 29 insertions, 9 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()); +} diff --git a/chrome/browser/views/first_run_search_engine_view.cc b/chrome/browser/views/first_run_search_engine_view.cc index 6fb8081..6554fc6 100644 --- a/chrome/browser/views/first_run_search_engine_view.cc +++ b/chrome/browser/views/first_run_search_engine_view.cc @@ -6,10 +6,12 @@ #include <algorithm> #include <map> +#include <vector> #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "base/i18n/rtl.h" +#include "base/rand_util.h" #include "base/time.h" #include "chrome/browser/options_window.h" #include "chrome/browser/profile.h" @@ -221,10 +223,9 @@ void FirstRunSearchEngineView::OnTemplateURLModelChanged() { // Randomize order of logos if option has been set. if (randomize_) { - int seed = static_cast<int>(Time::Now().ToInternalValue()); - srand(seed); std::random_shuffle(search_engine_choices_.begin(), - search_engine_choices_.end()); + search_engine_choices_.end(), + base::RandGenerator); // Assign to each choice the position in which it is shown on the screen. std::vector<SearchEngineChoice*>::iterator it; int slot = 0; diff --git a/net/websockets/websocket_handshake.cc b/net/websockets/websocket_handshake.cc index e6f0aa8..a9f0c35 100644 --- a/net/websockets/websocket_handshake.cc +++ b/net/websockets/websocket_handshake.cc @@ -70,7 +70,7 @@ std::string WebSocketHandshake::CreateClientHandshakeMessage() { fields.push_back("Sec-WebSocket-Key1: " + parameter_->GetSecWebSocketKey1()); fields.push_back("Sec-WebSocket-Key2: " + parameter_->GetSecWebSocketKey2()); - std::random_shuffle(fields.begin(), fields.end()); + std::random_shuffle(fields.begin(), fields.end(), base::RandGenerator); for (size_t i = 0; i < fields.size(); i++) { msg += fields[i] + "\r\n"; |