Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1599)

Unified Diff: chrome/browser/metrics/random_selector.h

Issue 1334943003: metrics: Add RandomSelector, a class that randomly selects items with odds (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add constructor for OddsAndValue Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/metrics/random_selector.cc » ('j') | chrome/browser/metrics/random_selector.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/metrics/random_selector.h
diff --git a/chrome/browser/metrics/random_selector.h b/chrome/browser/metrics/random_selector.h
new file mode 100644
index 0000000000000000000000000000000000000000..6e68a20563741788d71f287dc366376362fdb14b
--- /dev/null
+++ b/chrome/browser/metrics/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.
+
+#include <string>
+#include <vector>
+
+#ifndef CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
+#define CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
+
+namespace metrics {
+
+// 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 GetNext().
+//
+// Sample usage:
+//
+// RandomSelector random_selector;
+// std::vector<RandomSelector::OddsAndValue> 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.GetNext();
+class RandomSelector {
+ public:
+ struct OddsAndValue {
+ template <size_t N>
+ OddsAndValue(double weight_, const char* (&array)[N])
Alexei Svitkine (slow) 2015/09/11 21:00:54 Nit: No _'s for params. I think using the same nam
dhsharp 2015/09/11 22:53:25 Oh, okay. Was thinking that wouldn't work. Done.
+ : weight(weight_), value(array, array + N) {
+ }
+
+ double weight;
+ std::vector<std::string> value;
+ };
+
+ RandomSelector();
+ virtual ~RandomSelector();
+
+ // Set the probabilities for various strings.
+ void SetOdds(const std::vector<OddsAndValue>& odds);
+
+ // Get the next randomly picked string in |next|.
+ const std::vector<std::string>& GetNext();
+
+ // Returns the number of string entries.
+ size_t GetNumValues() const {
+ return odds_.size();
+ }
+
+ // Sum of the |weight| fields in the vector.
+ static double SumOdds(const std::vector<OddsAndValue>& odds);
+
+ private:
+ // Get a floating point number between 0.0 and |max|.
+ virtual double RandDoubleUpTo(double max);
+
+ // Get a string corresponding to a random double |value| that is in our odds
+ // vector. Stores the result in |key|.
+ const std::vector<std::string>& GetKeyOf(double value);
+
+ // A dictionary representing the strings to choose from and associated odds.
+ std::vector<OddsAndValue> odds_;
+
+ double sum_of_odds_;
+};
Alexei Svitkine (slow) 2015/09/11 21:00:54 DISALLOW_COPY_AND_ASSIGN().
dhsharp 2015/09/11 22:53:25 Done.
+
+} // namespace metrics
+
+#endif // CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
« no previous file with comments | « no previous file | chrome/browser/metrics/random_selector.cc » ('j') | chrome/browser/metrics/random_selector.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698