Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame^] | 1 | // 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 | |
| 8 | #include <cstdlib> |
| 9 | |
| 10 | #include <base/basictypes.h> |
| 11 | |
| 12 | namespace chromeos_policy_manager { |
| 13 | |
| 14 | // An unsecure Pseudo-Random Number Generator class based on rand_r(3), which |
| 15 | // is thread safe and doesn't interfere with other calls to rand(). |
| 16 | class PRNG { |
| 17 | public: |
| 18 | // Creates the object using the passed |seed| value as the initial state. |
| 19 | explicit PRNG(unsigned int seed) : state_(seed) {} |
| 20 | |
| 21 | // Returns a pseudo-random integer in the range [0, RAND_MAX]. |
| 22 | int rand() { return rand_r(&state_); } |
| 23 | |
| 24 | private: |
| 25 | // The internal state of the PRNG. |
| 26 | unsigned int state_; |
| 27 | |
| 28 | DISALLOW_COPY_AND_ASSIGN(PRNG); |
| 29 | }; |
| 30 | |
| 31 | } // namespace chromeos_policy_manager |
| 32 | |
| 33 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PRNG_H_ |