blob: bae8c311782bc7374796c53e35887c6c1cc47ccc [file] [log] [blame]
[email protected]ead8c1f2012-05-30 14:26:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]05f9b682008-09-29 22:18:012// 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
[email protected]29548d82011-04-29 21:03:548#include <string>
9
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
[email protected]05f9b682008-09-29 22:18:0111#include "base/basictypes.h"
12
13namespace base {
14
15// Returns a random number in range [0, kuint64max]. Thread-safe.
[email protected]0bea7252011-08-05 15:34:0016BASE_EXPORT uint64 RandUint64();
[email protected]05f9b682008-09-29 22:18:0117
18// Returns a random number between min and max (inclusive). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0019BASE_EXPORT int RandInt(int min, int max);
[email protected]05f9b682008-09-29 22:18:0120
[email protected]0173b962011-08-24 19:58:3621// Returns a random number in range [0, range). Thread-safe.
[email protected]a74dcae2010-08-30 21:07:0522//
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);
[email protected]0173b962011-08-24 19:58:3626BASE_EXPORT uint64 RandGenerator(uint64 range);
[email protected]a74dcae2010-08-30 21:07:0527
[email protected]05f9b682008-09-29 22:18:0128// Returns a random double in range [0, 1). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0029BASE_EXPORT double RandDouble();
[email protected]05f9b682008-09-29 22:18:0130
[email protected]edafd4c2011-05-10 17:18:5331// Given input |bits|, convert with maximum precision to a double in
32// the range [0, 1). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0033BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits);
[email protected]edafd4c2011-05-10 17:18:5334
[email protected]9b205782012-08-02 20:22:2535// 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.
[email protected]0bea7252011-08-05 15:34:0040BASE_EXPORT void RandBytes(void* output, size_t output_length);
[email protected]51a01812011-05-05 08:46:1141
[email protected]9b205782012-08-02 20:22:2542// Fills a string of length |length| with with random data and returns it.
43// |length| should be nonzero.
[email protected]51a01812011-05-05 08:46:1144//
[email protected]fdce4782011-11-29 20:06:1845// Note that this is a variation of |RandBytes| with a different return type.
[email protected]9b205782012-08-02 20:22:2546//
47// WARNING:
48// Do not use for security-sensitive purposes.
49// See crypto/ for cryptographically secure random number generation APIs.
[email protected]0bea7252011-08-05 15:34:0050BASE_EXPORT std::string RandBytesAsString(size_t length);
[email protected]29548d82011-04-29 21:03:5451
[email protected]31662202013-03-23 19:10:5452#if defined(OS_POSIX)
[email protected]ead8c1f2012-05-30 14:26:1353BASE_EXPORT int GetUrandomFD();
54#endif
55
[email protected]05f9b682008-09-29 22:18:0156} // namespace base
57
[email protected]2fdc86a2010-01-26 23:08:0258#endif // BASE_RAND_UTIL_H_