[email protected] | f5661ca | 2011-03-24 19:00:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 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 BASE_RAND_UTIL_H_ |
| 6 | #define BASE_RAND_UTIL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 8 | |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | // Returns a random number in range [0, kuint64max]. Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 17 | BASE_EXPORT uint64 RandUint64(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 18 | |
| 19 | // Returns a random number between min and max (inclusive). Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 20 | BASE_EXPORT int RandInt(int min, int max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 21 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame^] | 22 | // Returns a random number in range [0, range). Thread-safe. |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 23 | // |
| 24 | // Note that this can be used as an adapter for std::random_shuffle(): |
| 25 | // Given a pre-populated |std::vector<int> myvector|, shuffle it as |
| 26 | // std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator); |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame^] | 27 | BASE_EXPORT uint64 RandGenerator(uint64 range); |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 28 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 29 | // Returns a random double in range [0, 1). Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 30 | BASE_EXPORT double RandDouble(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 31 | |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 32 | // Given input |bits|, convert with maximum precision to a double in |
| 33 | // the range [0, 1). Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 34 | BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits); |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 35 | |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 36 | // Fills |output_length| bytes of |output| with cryptographically strong random |
| 37 | // data. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 38 | BASE_EXPORT void RandBytes(void* output, size_t output_length); |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 39 | |
| 40 | // Fills a string of length |length| with with cryptographically strong random |
| 41 | // data and returns it. |
| 42 | // |
| 43 | // Not that this is a variation of |RandBytes| with a different return type. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 44 | BASE_EXPORT std::string RandBytesAsString(size_t length); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 45 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 46 | } // namespace base |
| 47 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 48 | #endif // BASE_RAND_UTIL_H_ |