Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 16 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 19 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 20 | #include <random> |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 21 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 22 | #include <base/logging.h> |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 23 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 24 | namespace chromeos_update_manager { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 25 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 26 | // A thread-safe, unsecure, 32-bit pseudo-random number generator based on |
| 27 | // std::mt19937. |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 28 | class PRNG { |
| 29 | public: |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 30 | // Initializes the generator with the passed |seed| value. |
| 31 | explicit PRNG(uint32_t seed) : gen_(seed) {} |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 32 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 33 | // Returns a random unsigned 32-bit integer. |
| 34 | uint32_t Rand() { return gen_(); } |
| 35 | |
| 36 | // Returns a random integer uniformly distributed in the range [min, max]. |
| 37 | int RandMinMax(int min, int max) { |
| 38 | DCHECK_LE(min, max); |
| 39 | return std::uniform_int_distribution<>(min, max)(gen_); |
| 40 | } |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 41 | |
| 42 | private: |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 43 | // A pseudo-random number generator. |
| 44 | std::mt19937 gen_; |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(PRNG); |
| 47 | }; |
| 48 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 49 | } // namespace chromeos_update_manager |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 50 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 51 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ |