blob: ca07e5ecd88ee98d093abd58a79f79bbde4b18e9 [file] [log] [blame]
Alex Deymoedfa1d42014-04-28 16:53:51 -07001// Copyright (c) 2014 The Chromium OS 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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_
Alex Deymoedfa1d42014-04-28 16:53:51 -07007
Gilad Arnolde1218812014-05-07 12:21:36 -07008#include <random>
Alex Deymoedfa1d42014-04-28 16:53:51 -07009
Gilad Arnolde1218812014-05-07 12:21:36 -070010#include <base/logging.h>
Alex Deymoedfa1d42014-04-28 16:53:51 -070011
Alex Deymo63784a52014-05-28 10:46:14 -070012namespace chromeos_update_manager {
Alex Deymoedfa1d42014-04-28 16:53:51 -070013
Gilad Arnolde1218812014-05-07 12:21:36 -070014// A thread-safe, unsecure, 32-bit pseudo-random number generator based on
15// std::mt19937.
Alex Deymoedfa1d42014-04-28 16:53:51 -070016class PRNG {
17 public:
Gilad Arnolde1218812014-05-07 12:21:36 -070018 // Initializes the generator with the passed |seed| value.
19 explicit PRNG(uint32_t seed) : gen_(seed) {}
Alex Deymoedfa1d42014-04-28 16:53:51 -070020
Gilad Arnolde1218812014-05-07 12:21:36 -070021 // Returns a random unsigned 32-bit integer.
22 uint32_t Rand() { return gen_(); }
23
24 // Returns a random integer uniformly distributed in the range [min, max].
25 int RandMinMax(int min, int max) {
26 DCHECK_LE(min, max);
27 return std::uniform_int_distribution<>(min, max)(gen_);
28 }
Alex Deymoedfa1d42014-04-28 16:53:51 -070029
30 private:
Gilad Arnolde1218812014-05-07 12:21:36 -070031 // A pseudo-random number generator.
32 std::mt19937 gen_;
Alex Deymoedfa1d42014-04-28 16:53:51 -070033
34 DISALLOW_COPY_AND_ASSIGN(PRNG);
35};
36
Alex Deymo63784a52014-05-28 10:46:14 -070037} // namespace chromeos_update_manager
Alex Deymoedfa1d42014-04-28 16:53:51 -070038
Alex Deymo63784a52014-05-28 10:46:14 -070039#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_