| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/perf/random_selector.h" | 5 #include "chrome/browser/metrics/perf/random_selector.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 | 12 |
| 13 RandomSelector::RandomSelector() : sum_of_weights_(0) {} | 13 RandomSelector::RandomSelector() : sum_of_weights_(0) {} |
| 14 | 14 |
| 15 RandomSelector::~RandomSelector() {} | 15 RandomSelector::~RandomSelector() {} |
| 16 | 16 |
| 17 // static |
| 17 double RandomSelector::SumWeights(const std::vector<WeightAndValue>& odds) { | 18 double RandomSelector::SumWeights(const std::vector<WeightAndValue>& odds) { |
| 18 double sum = 0.0; | 19 double sum = 0.0; |
| 19 for (const auto& odd : odds) { | 20 for (const auto& odd : odds) { |
| 21 if (odd.weight <= 0.0) |
| 22 return -1.0; |
| 20 sum += odd.weight; | 23 sum += odd.weight; |
| 21 } | 24 } |
| 22 return sum; | 25 return sum; |
| 23 } | 26 } |
| 24 | 27 |
| 25 void RandomSelector::SetOdds(const std::vector<WeightAndValue>& odds) { | 28 bool RandomSelector::SetOdds(const std::vector<WeightAndValue>& odds) { |
| 29 double sum = SumWeights(odds); |
| 30 if (sum <= 0.0) |
| 31 return false; |
| 26 odds_ = odds; | 32 odds_ = odds; |
| 27 sum_of_weights_ = SumWeights(odds_); | 33 sum_of_weights_ = sum; |
| 34 return true; |
| 28 } | 35 } |
| 29 | 36 |
| 30 const std::string& RandomSelector::Select() { | 37 const std::string& RandomSelector::Select() { |
| 31 // Get a random double between 0 and the sum. | 38 // Get a random double between 0 and the sum. |
| 32 double random = RandDoubleUpTo(sum_of_weights_); | 39 double random = RandDoubleUpTo(sum_of_weights_); |
| 33 // Figure out what it belongs to. | 40 // Figure out what it belongs to. |
| 34 return GetValueFor(random); | 41 return GetValueFor(random); |
| 35 } | 42 } |
| 36 | 43 |
| 37 double RandomSelector::RandDoubleUpTo(double max) { | 44 double RandomSelector::RandDoubleUpTo(double max) { |
| 38 CHECK_GT(max, 0.0); | 45 CHECK_GT(max, 0.0); |
| 39 return max * base::RandDouble(); | 46 return max * base::RandDouble(); |
| 40 } | 47 } |
| 41 | 48 |
| 42 const std::string& RandomSelector::GetValueFor(double random) { | 49 const std::string& RandomSelector::GetValueFor(double random) { |
| 43 double current = 0.0; | 50 double current = 0.0; |
| 44 for (const auto& odd : odds_) { | 51 for (const auto& odd : odds_) { |
| 45 current += odd.weight; | 52 current += odd.weight; |
| 46 if (random < current) | 53 if (random < current) |
| 47 return odd.value; | 54 return odd.value; |
| 48 } | 55 } |
| 49 NOTREACHED() << "Invalid value for key: " << random; | 56 NOTREACHED() << "Invalid value for key: " << random; |
| 50 return base::EmptyString(); | 57 return base::EmptyString(); |
| 51 } | 58 } |
| 59 |
| 60 ::std::ostream& operator<<( |
| 61 ::std::ostream& os, const RandomSelector::WeightAndValue& value) { |
| 62 return os << "{" << value.weight << ", \"" << value.value << "\"}"; |
| 63 } |
| OLD | NEW |