Chromium Code Reviews| Index: chrome/browser/metrics/perf/random_selector.h |
| diff --git a/chrome/browser/metrics/perf/random_selector.h b/chrome/browser/metrics/perf/random_selector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6af61d148d9a2a625c7e98227755b82757272574 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/perf/random_selector.h |
| @@ -0,0 +1,77 @@ |
| +// 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. |
| + |
| +#ifndef CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
| +#define CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| + |
| +// RandomSelector can be used to pick vectors of strings according to certain |
| +// probabilities. The probabilities are set using SetOdds(). A randomly picked |
| +// vector can be obtained by calling Select(). |
| +// |
| +// Sample usage: |
| +// |
| +// RandomSelector random_selector; |
| +// std::vector<RandomSelector::WeightAndValue> odds { |
| +// {50, "a"}, |
| +// {40, "b"}, |
| +// {10, "c"} |
| +// }; |
| +// random_selector.SetOdds(odds); |
| +// |
| +// // The following should return "a" with a probability of 50%, |
| +// // "b" with a probability of 40%, and "c" with a probability of 10%: |
| +// |
| +// std::vector<std::string>& selection = random_selector.Select(); |
|
Alexei Svitkine (slow)
2015/09/15 18:36:12
Nit: I find the nested comment above a bit messy.
dhsharp
2015/09/16 01:01:40
Done.
|
| +class RandomSelector { |
| + public: |
| + struct WeightAndValue { |
| + WeightAndValue(double weight, const std::string& value) |
| + : weight(weight), value(value) { |
| + } |
| + |
| + double weight; |
|
Alexei Svitkine (slow)
2015/09/15 18:36:12
Nit: Add a comment mention it's the probability we
dhsharp
2015/09/16 01:01:40
Done.
|
| + std::string value; |
| + }; |
| + |
| + RandomSelector(); |
| + virtual ~RandomSelector(); |
| + |
| + // Set the probabilities for various strings. |
| + void SetOdds(const std::vector<WeightAndValue>& odds); |
| + |
| + // Randomly select one of the values from the set. |
| + const std::string& Select(); |
| + |
| + // Returns the number of string entries. |
| + size_t num_values() const { |
| + return odds_.size(); |
| + } |
| + |
| + // Sum of the |weight| fields in the vector. |
| + static double SumWeights(const std::vector<WeightAndValue>& odds); |
| + |
| + private: |
| + // Get a floating point number between 0.0 and |max|. |
| + virtual double RandDoubleUpTo(double max); |
| + |
| + // Get a string corresponding to |random| that is in the odds vector. |
| + // |random| must be a number between zero and the sum of the probability |
| + // weights. |
| + const std::string& GetValueFor(double random); |
| + |
| + // A dictionary representing the strings to choose from and associated odds. |
| + std::vector<WeightAndValue> odds_; |
| + |
| + // Sum of the probability weights. |
| + double sum_of_weights_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RandomSelector); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |