blob: d415f959713ecaeeb032e36c48b0a3f283205e92 [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
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
avibf0746c2015-12-09 19:53:149#include <stdint.h>
10
[email protected]29548d82011-04-29 21:03:5411#include <string>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
avibf0746c2015-12-09 19:53:1414#include "build/build_config.h"
[email protected]05f9b682008-09-29 22:18:0115
16namespace base {
17
avibf0746c2015-12-09 19:53:1418// Returns a random number in range [0, UINT64_MAX]. Thread-safe.
19BASE_EXPORT uint64_t RandUint64();
[email protected]05f9b682008-09-29 22:18:0120
21// Returns a random number between min and max (inclusive). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0022BASE_EXPORT int RandInt(int min, int max);
[email protected]05f9b682008-09-29 22:18:0123
[email protected]0173b962011-08-24 19:58:3624// Returns a random number in range [0, range). Thread-safe.
[email protected]a74dcae2010-08-30 21:07:0525//
26// Note that this can be used as an adapter for std::random_shuffle():
27// Given a pre-populated |std::vector<int> myvector|, shuffle it as
28// std::random_shuffle(myvector.begin(), myvector.end(), base::RandGenerator);
avibf0746c2015-12-09 19:53:1429BASE_EXPORT uint64_t RandGenerator(uint64_t range);
[email protected]a74dcae2010-08-30 21:07:0530
[email protected]05f9b682008-09-29 22:18:0131// Returns a random double in range [0, 1). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0032BASE_EXPORT double RandDouble();
[email protected]05f9b682008-09-29 22:18:0133
[email protected]edafd4c2011-05-10 17:18:5334// Given input |bits|, convert with maximum precision to a double in
35// the range [0, 1). Thread-safe.
avibf0746c2015-12-09 19:53:1436BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64_t bits);
[email protected]edafd4c2011-05-10 17:18:5337
John Mellorafab9722017-09-26 16:28:1938// Fills |output_length| bytes of |output| with random data. Thread-safe.
[email protected]9b205782012-08-02 20:22:2539//
John Mellorafab9722017-09-26 16:28:1940// Although implementations are required to use a cryptographically secure
41// random number source, code outside of base/ that relies on this should use
42// crypto::RandBytes instead to ensure the requirement is easily discoverable.
[email protected]0bea7252011-08-05 15:34:0043BASE_EXPORT void RandBytes(void* output, size_t output_length);
[email protected]51a01812011-05-05 08:46:1144
[email protected]3818dd02014-05-13 05:56:1945// Fills a string of length |length| with random data and returns it.
John Mellorafab9722017-09-26 16:28:1946// |length| should be nonzero. Thread-safe.
[email protected]51a01812011-05-05 08:46:1147//
[email protected]fdce4782011-11-29 20:06:1848// Note that this is a variation of |RandBytes| with a different return type.
[email protected]3818dd02014-05-13 05:56:1949// The returned string is likely not ASCII/UTF-8. Use with care.
[email protected]9b205782012-08-02 20:22:2550//
John Mellorafab9722017-09-26 16:28:1951// Although implementations are required to use a cryptographically secure
52// random number source, code outside of base/ that relies on this should use
53// crypto::RandBytes instead to ensure the requirement is easily discoverable.
[email protected]0bea7252011-08-05 15:34:0054BASE_EXPORT std::string RandBytesAsString(size_t length);
[email protected]29548d82011-04-29 21:03:5455
Scott Graham4ffd63b52017-06-01 18:03:3356#if defined(OS_POSIX) && !defined(OS_FUCHSIA)
[email protected]ead8c1f2012-05-30 14:26:1357BASE_EXPORT int GetUrandomFD();
58#endif
59
[email protected]05f9b682008-09-29 22:18:0160} // namespace base
61
[email protected]2fdc86a2010-01-26 23:08:0262#endif // BASE_RAND_UTIL_H_