Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium 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 BASE_RAND_UTIL_H_ |
| 6 | #define BASE_RAND_UTIL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/basictypes.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | // Returns a random number in range [0, kuint64max]. Thread-safe. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 16 | uint64 RandUint64(); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 17 | |
| 18 | // Returns a random number between min and max (inclusive). Thread-safe. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 19 | int RandInt(int min, int max); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 20 | |
| 21 | // Returns a random number in range [0, range). Thread-safe. |
| 22 | // |
| 23 | // Note that this can be used as an adapter for std::random_shuffle(): |
| 24 | // Given a pre-populated |std::vector<int> myvector|, shuffle it as |
| 25 | // std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator); |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 26 | uint64 RandGenerator(uint64 range); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 27 | |
| 28 | // Returns a random double in range [0, 1). Thread-safe. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 29 | double RandDouble(); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 30 | |
| 31 | // Given input |bits|, convert with maximum precision to a double in |
| 32 | // the range [0, 1). Thread-safe. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 33 | double BitsToOpenEndedUnitInterval(uint64 bits); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 34 | |
| 35 | // Fills |output_length| bytes of |output| with random data. |
| 36 | // |
| 37 | // WARNING: |
| 38 | // Do not use for security-sensitive purposes. |
| 39 | // See crypto/ for cryptographically secure random number generation APIs. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 40 | void RandBytes(void* output, size_t output_length); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 41 | |
| 42 | // Fills a string of length |length| with random data and returns it. |
| 43 | // |length| should be nonzero. |
| 44 | // |
| 45 | // Note that this is a variation of |RandBytes| with a different return type. |
| 46 | // The returned string is likely not ASCII/UTF-8. Use with care. |
| 47 | // |
| 48 | // WARNING: |
| 49 | // Do not use for security-sensitive purposes. |
| 50 | // See crypto/ for cryptographically secure random number generation APIs. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 51 | std::string RandBytesAsString(size_t length); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 52 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 53 | } // namespace base |
| 54 | |
| 55 | #endif // BASE_RAND_UTIL_H_ |