File: random_selector.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 The Chromium Authors
// 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 <stddef.h>

#include <string>
#include <vector>

// 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);
//
// std::vector<std::string>& selection = random_selector.Select();
//
// The above line should return "a" with a probability of 50%,
// "b" with a probability of 40%, and "c" with a probability of 10%:
class RandomSelector {
 public:
  struct WeightAndValue {
    WeightAndValue(double weight, const std::string& value)
      : weight(weight), value(value) {
    }

    bool operator==(const WeightAndValue& other) const {
      return weight == other.weight && value == other.value;
    }

    // Probability weight for selecting this value.
    double weight;
    // Value to be returned by Select(), if selected.
    std::string value;
  };

  RandomSelector();

  RandomSelector(const RandomSelector&) = delete;
  RandomSelector& operator=(const RandomSelector&) = delete;

  virtual ~RandomSelector();

  // Set the probabilities for various strings. Returns false and doesn't
  // modify the values if odds contains any invalid weights (<=0.0) or if
  // odds is empty.
  bool 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();
  }

  const std::vector<WeightAndValue>& odds() const { return odds_; }

  // Sum of the |weight| fields in the vector. Returns -1.0 if odds contains any
  // weight <= 0.0.
  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_;
};

::std::ostream& operator<<(
    ::std::ostream& os, const RandomSelector::WeightAndValue& value);

#endif  // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_