WebKit roll 98705:98715
[chromium-blink-merge.git] / base / rand_util.h
blob4474c02beaa336976fd637cf1d08c478fddd1595
1 // Copyright (c) 2011 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.
5 #ifndef BASE_RAND_UTIL_H_
6 #define BASE_RAND_UTIL_H_
7 #pragma once
9 #include <string>
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
14 namespace base {
16 // Returns a random number in range [0, kuint64max]. Thread-safe.
17 BASE_EXPORT uint64 RandUint64();
19 // Returns a random number between min and max (inclusive). Thread-safe.
20 BASE_EXPORT int RandInt(int min, int max);
22 // Returns a random number in range [0, range). Thread-safe.
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);
27 BASE_EXPORT uint64 RandGenerator(uint64 range);
29 // Returns a random double in range [0, 1). Thread-safe.
30 BASE_EXPORT double RandDouble();
32 // Given input |bits|, convert with maximum precision to a double in
33 // the range [0, 1). Thread-safe.
34 BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits);
36 // Fills |output_length| bytes of |output| with cryptographically strong random
37 // data.
38 BASE_EXPORT void RandBytes(void* output, size_t output_length);
40 // Fills a string of length |length| with with cryptographically strong random
41 // data and returns it.
43 // Not that this is a variation of |RandBytes| with a different return type.
44 BASE_EXPORT std::string RandBytesAsString(size_t length);
46 } // namespace base
48 #endif // BASE_RAND_UTIL_H_