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..5ae7b9c5b39f7a6a8591577d96b38ba7e7195fa8 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/perf/random_selector.h |
| @@ -0,0 +1,76 @@ |
| +// 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(); |
| +class RandomSelector { |
| + public: |
| + struct WeightAndValue { |
| + WeightAndValue(double weight, const std::string& value) |
| + : weight(weight), value(value) { |
| + } |
| + |
| + double weight; |
| + std::string value; |
| + }; |
| + |
| + RandomSelector(); |
| + virtual ~RandomSelector(); |
| + |
| + // Set the probabilities for various strings. |
| + void SetOdds(const std::vector<WeightAndValue>& odds); |
| + |
| + // Get the next randomly picked string. |
|
Alexei Svitkine (slow)
2015/09/15 15:31:18
Nit: Update comment (don't use the term "next" sin
dhsharp
2015/09/15 18:21:32
Done.
|
| + 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_; |
| + |
| + double sum_of_odds_; |
|
Alexei Svitkine (slow)
2015/09/15 15:31:18
Nit: Add a comment.
dhsharp
2015/09/15 18:21:32
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(RandomSelector); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |