michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_RAND_UTIL_H_ michael@0: #define BASE_RAND_UTIL_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace base { michael@0: michael@0: // Returns a random number in range [0, kuint64max]. Thread-safe. michael@0: BASE_EXPORT uint64 RandUint64(); michael@0: michael@0: // Returns a random number between min and max (inclusive). Thread-safe. michael@0: BASE_EXPORT int RandInt(int min, int max); michael@0: michael@0: // Returns a random number in range [0, range). Thread-safe. michael@0: // michael@0: // Note that this can be used as an adapter for std::random_shuffle(): michael@0: // Given a pre-populated |std::vector myvector|, shuffle it as michael@0: // std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator); michael@0: BASE_EXPORT uint64 RandGenerator(uint64 range); michael@0: michael@0: // Returns a random double in range [0, 1). Thread-safe. michael@0: BASE_EXPORT double RandDouble(); michael@0: michael@0: // Given input |bits|, convert with maximum precision to a double in michael@0: // the range [0, 1). Thread-safe. michael@0: BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits); michael@0: michael@0: // Fills |output_length| bytes of |output| with random data. michael@0: // michael@0: // WARNING: michael@0: // Do not use for security-sensitive purposes. michael@0: // See crypto/ for cryptographically secure random number generation APIs. michael@0: BASE_EXPORT void RandBytes(void* output, size_t output_length); michael@0: michael@0: // Fills a string of length |length| with with random data and returns it. michael@0: // |length| should be nonzero. michael@0: // michael@0: // Note that this is a variation of |RandBytes| with a different return type. michael@0: // michael@0: // WARNING: michael@0: // Do not use for security-sensitive purposes. michael@0: // See crypto/ for cryptographically secure random number generation APIs. michael@0: BASE_EXPORT std::string RandBytesAsString(size_t length); michael@0: michael@0: #if defined(OS_POSIX) michael@0: BASE_EXPORT int GetUrandomFD(); michael@0: #endif michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_RAND_UTIL_H_