OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_RAND_UTIL_H_ | 5 #ifndef BASE_RAND_UTIL_H_ |
6 #define BASE_RAND_UTIL_H_ | 6 #define BASE_RAND_UTIL_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <string> | 10 #include <string> |
9 | 11 |
10 #include "base/base_export.h" | 12 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 13 #include "build/build_config.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 | 16 |
15 // Returns a random number in range [0, kuint64max]. Thread-safe. | 17 // Returns a random number in range [0, UINT64_MAX]. Thread-safe. |
16 BASE_EXPORT uint64 RandUint64(); | 18 BASE_EXPORT uint64_t RandUint64(); |
17 | 19 |
18 // Returns a random number between min and max (inclusive). Thread-safe. | 20 // Returns a random number between min and max (inclusive). Thread-safe. |
19 BASE_EXPORT int RandInt(int min, int max); | 21 BASE_EXPORT int RandInt(int min, int max); |
20 | 22 |
21 // Returns a random number in range [0, range). Thread-safe. | 23 // Returns a random number in range [0, range). Thread-safe. |
22 // | 24 // |
23 // Note that this can be used as an adapter for std::random_shuffle(): | 25 // 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 | 26 // Given a pre-populated |std::vector<int> myvector|, shuffle it as |
25 // std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator); | 27 // std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator); |
26 BASE_EXPORT uint64 RandGenerator(uint64 range); | 28 BASE_EXPORT uint64_t RandGenerator(uint64_t range); |
27 | 29 |
28 // Returns a random double in range [0, 1). Thread-safe. | 30 // Returns a random double in range [0, 1). Thread-safe. |
29 BASE_EXPORT double RandDouble(); | 31 BASE_EXPORT double RandDouble(); |
30 | 32 |
31 // Given input |bits|, convert with maximum precision to a double in | 33 // Given input |bits|, convert with maximum precision to a double in |
32 // the range [0, 1). Thread-safe. | 34 // the range [0, 1). Thread-safe. |
33 BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits); | 35 BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64_t bits); |
34 | 36 |
35 // Fills |output_length| bytes of |output| with random data. | 37 // Fills |output_length| bytes of |output| with random data. |
36 // | 38 // |
37 // WARNING: | 39 // WARNING: |
38 // Do not use for security-sensitive purposes. | 40 // Do not use for security-sensitive purposes. |
39 // See crypto/ for cryptographically secure random number generation APIs. | 41 // See crypto/ for cryptographically secure random number generation APIs. |
40 BASE_EXPORT void RandBytes(void* output, size_t output_length); | 42 BASE_EXPORT void RandBytes(void* output, size_t output_length); |
41 | 43 |
42 // Fills a string of length |length| with random data and returns it. | 44 // Fills a string of length |length| with random data and returns it. |
43 // |length| should be nonzero. | 45 // |length| should be nonzero. |
44 // | 46 // |
45 // Note that this is a variation of |RandBytes| with a different return type. | 47 // 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. | 48 // The returned string is likely not ASCII/UTF-8. Use with care. |
47 // | 49 // |
48 // WARNING: | 50 // WARNING: |
49 // Do not use for security-sensitive purposes. | 51 // Do not use for security-sensitive purposes. |
50 // See crypto/ for cryptographically secure random number generation APIs. | 52 // See crypto/ for cryptographically secure random number generation APIs. |
51 BASE_EXPORT std::string RandBytesAsString(size_t length); | 53 BASE_EXPORT std::string RandBytesAsString(size_t length); |
52 | 54 |
53 #if defined(OS_POSIX) | 55 #if defined(OS_POSIX) |
54 BASE_EXPORT int GetUrandomFD(); | 56 BASE_EXPORT int GetUrandomFD(); |
55 #endif | 57 #endif |
56 | 58 |
57 } // namespace base | 59 } // namespace base |
58 | 60 |
59 #endif // BASE_RAND_UTIL_H_ | 61 #endif // BASE_RAND_UTIL_H_ |
OLD | NEW |