blob: 2063bb2a2db8d6fa8d0f8bc05b61e20d984b2e14 [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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PRNG_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PRNG_H_
7
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
12namespace chromeos_policy_manager {
13
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
37} // namespace chromeos_policy_manager
38
39#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PRNG_H_