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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <string>
6 #include <vector>
7
8 #ifndef CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
9 #define CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
10
11 namespace metrics {
12
13 // RandomSelector can be used to pick vectors of strings according to certain
14 // probabilities. The probabilities are set using SetOdds(). A randomly picked
15 // vector can be obtained by calling GetNext().
16 //
17 // Sample usage:
18 //
19 // RandomSelector random_selector;
20 // std::vector<RandomSelector::OddsAndValue> odds {
21 // {50, {"a"}},
22 // {40, {"b"}},
23 // {10, {"c"}}
24 // };
25 // random_selector.SetOdds(odds);
26 //
27 // // The following should return "a" with a probability of 50%,
28 // // "b" with a probability of 40%, and "c" with a probability of 10%:
29 //
30 // std::vector<std::string>& selection = random_selector.GetNext();
31 class RandomSelector {
32 public:
33 struct OddsAndValue {
34 template <size_t N>
35 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.
36 : weight(weight_), value(array, array + N) {
37 }
38
39 double weight;
40 std::vector<std::string> value;
41 };
42
43 RandomSelector();
44 virtual ~RandomSelector();
45
46 // Set the probabilities for various strings.
47 void SetOdds(const std::vector<OddsAndValue>& odds);
48
49 // Get the next randomly picked string in |next|.
50 const std::vector<std::string>& GetNext();
51
52 // Returns the number of string entries.
53 size_t GetNumValues() const {
54 return odds_.size();
55 }
56
57 // Sum of the |weight| fields in the vector.
58 static double SumOdds(const std::vector<OddsAndValue>& odds);
59
60 private:
61 // Get a floating point number between 0.0 and |max|.
62 virtual double RandDoubleUpTo(double max);
63
64 // Get a string corresponding to a random double |value| that is in our odds
65 // vector. Stores the result in |key|.
66 const std::vector<std::string>& GetKeyOf(double value);
67
68 // A dictionary representing the strings to choose from and associated odds.
69 std::vector<OddsAndValue> odds_;
70
71 double sum_of_odds_;
72 };
Alexei Svitkine (slow) 2015/09/11 21:00:54 DISALLOW_COPY_AND_ASSIGN().
dhsharp 2015/09/11 22:53:25 Done.
73
74 } // namespace metrics
75
76 #endif // CHROME_BROWSER_METRICS_RANDOM_SELECTOR_H_
OLDNEW
« 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