Chromium Code Reviews| Index: chrome/browser/metrics/perf/random_selector.cc |
| diff --git a/chrome/browser/metrics/perf/random_selector.cc b/chrome/browser/metrics/perf/random_selector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..280b05423a715e09f08ee91fe02815c612a90090 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/perf/random_selector.cc |
| @@ -0,0 +1,54 @@ |
| +// Copyright 2015 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. |
| + |
| +#include "chrome/browser/metrics/perf/random_selector.h" |
| + |
| +#include <string> |
| +#include <vector> |
|
Alexei Svitkine (slow)
2015/09/15 15:31:18
Nit: These two are not needed, because the header
dhsharp
2015/09/15 18:21:32
Done.
|
| + |
| +#include "base/logging.h" |
| +#include "base/rand_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| + |
| +RandomSelector::RandomSelector() {} |
|
Alexei Svitkine (slow)
2015/09/15 15:31:18
Nit: Initialize sum_of_odds_ - or tools like cover
dhsharp
2015/09/15 18:21:32
Done.
|
| + |
| +RandomSelector::~RandomSelector() {} |
| + |
| +double RandomSelector::SumWeights(const std::vector<WeightAndValue>& odds) { |
| + double sum = 0.0; |
| + for (const auto& odd : odds) { |
| + sum += odd.weight; |
| + } |
| + return sum; |
| +} |
| + |
| +void RandomSelector::SetOdds(const std::vector<WeightAndValue>& odds) { |
| + odds_ = odds; |
| + sum_of_odds_ = SumWeights(odds_); |
| +} |
| + |
| +const std::string& RandomSelector::Select() { |
| + // Get a random double between 0 and the sum. |
| + double random = RandDoubleUpTo(sum_of_odds_); |
| + // Figure out what it belongs to. |
| + return GetValueFor(random); |
| +} |
| + |
| +double RandomSelector::RandDoubleUpTo(double max) { |
| + CHECK_GT(max, 0.0); |
| + return max * base::RandDouble(); |
| +} |
| + |
| +const std::string& RandomSelector::GetValueFor(double random) { |
| + double current = 0.0; |
| + for (const auto& odd : odds_) { |
| + current += odd.weight; |
| + if (random < current) |
| + return odd.value; |
| + } |
| + NOTREACHED() << "Invalid value for key: " << random; |
| + return base::EmptyString(); |
| +} |