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// 16 17#ifndef UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ 18#define UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ 19 20#include <random> 21 22#include <base/logging.h> 23 24namespace chromeos_update_manager { 25 26// A thread-safe, unsecure, 32-bit pseudo-random number generator based on 27// std::mt19937. 28class PRNG { 29 public: 30 // Initializes the generator with the passed |seed| value. 31 explicit PRNG(uint32_t seed) : gen_(seed) {} 32 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 } 41 42 private: 43 // A pseudo-random number generator. 44 std::mt19937 gen_; 45 46 DISALLOW_COPY_AND_ASSIGN(PRNG); 47}; 48 49} // namespace chromeos_update_manager 50 51#endif // UPDATE_ENGINE_UPDATE_MANAGER_PRNG_H_ 52