noisy_metrics_recorder.h 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2021 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. #ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_NOISY_METRICS_RECORDER_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_NOISY_METRICS_RECORDER_H_
  6. #include <stdint.h>
  7. // NoisyMetricsRecorder can be used to add noise to metrics before recording
  8. // them.
  9. class NoisyMetricsRecorder {
  10. public:
  11. NoisyMetricsRecorder();
  12. ~NoisyMetricsRecorder();
  13. // |flip_probability| is the probability that an individual bit will get
  14. // flipped. Typical value of flip_probability is 0.5. |original_metric| is the
  15. // metric that contains a value that typically needs at most |count_bits| to
  16. // be expressed. The method returns the flipped value of |original_metric|.
  17. uint32_t GetNoisyMetric(float flip_probability,
  18. uint32_t original_metric,
  19. uint8_t count_bits);
  20. protected:
  21. // Returns a random float between 0 and 1 (both inclusive).
  22. virtual float GetRandBetween0And1() const;
  23. // Returns a random number that's either 0 or 1.
  24. virtual int GetRandEither0Or1() const;
  25. };
  26. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_NOISY_METRICS_RECORDER_H_