blob: 6130c129b7fa615d68014829fa6bc400abd329be [file] [log] [blame]
mseaborn@chromium.org056521b2012-05-30 23:26:13 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mark@chromium.orgb93c0542008-09-30 07:18:01 +09002// 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
abarth@chromium.orgc19cb312011-04-30 06:03:54 +09008#include <string>
9
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
mark@chromium.orgb93c0542008-09-30 07:18:01 +090011#include "base/basictypes.h"
12
13namespace base {
14
15// Returns a random number in range [0, kuint64max]. Thread-safe.
darin@chromium.orge585bed2011-08-06 00:34:00 +090016BASE_EXPORT uint64 RandUint64();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090017
18// Returns a random number between min and max (inclusive). Thread-safe.
darin@chromium.orge585bed2011-08-06 00:34:00 +090019BASE_EXPORT int RandInt(int min, int max);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090020
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090021// Returns a random number in range [0, range). Thread-safe.
isherman@chromium.org01eaa0b2010-08-31 06:07:05 +090022//
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);
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090026BASE_EXPORT uint64 RandGenerator(uint64 range);
isherman@chromium.org01eaa0b2010-08-31 06:07:05 +090027
mark@chromium.orgb93c0542008-09-30 07:18:01 +090028// Returns a random double in range [0, 1). Thread-safe.
darin@chromium.orge585bed2011-08-06 00:34:00 +090029BASE_EXPORT double RandDouble();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090030
joi@chromium.orgdf3561d2011-05-11 02:18:53 +090031// Given input |bits|, convert with maximum precision to a double in
32// the range [0, 1). Thread-safe.
darin@chromium.orge585bed2011-08-06 00:34:00 +090033BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64 bits);
joi@chromium.orgdf3561d2011-05-11 02:18:53 +090034
mniknami@chromium.orgf4331ae2012-08-03 05:22:25 +090035// 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.
darin@chromium.orge585bed2011-08-06 00:34:00 +090040BASE_EXPORT void RandBytes(void* output, size_t output_length);
qsr@google.comfbda4d52011-05-05 17:46:11 +090041
thestig@chromium.orgce9eaed2014-05-13 14:56:19 +090042// Fills a string of length |length| with random data and returns it.
mniknami@chromium.orgf4331ae2012-08-03 05:22:25 +090043// |length| should be nonzero.
qsr@google.comfbda4d52011-05-05 17:46:11 +090044//
pkasting@chromium.orgd3479372011-11-30 05:06:18 +090045// Note that this is a variation of |RandBytes| with a different return type.
thestig@chromium.orgce9eaed2014-05-13 14:56:19 +090046// The returned string is likely not ASCII/UTF-8. Use with care.
mniknami@chromium.orgf4331ae2012-08-03 05:22:25 +090047//
48// WARNING:
49// Do not use for security-sensitive purposes.
50// See crypto/ for cryptographically secure random number generation APIs.
darin@chromium.orge585bed2011-08-06 00:34:00 +090051BASE_EXPORT std::string RandBytesAsString(size_t length);
abarth@chromium.orgc19cb312011-04-30 06:03:54 +090052
dbeam@chromium.orge8888492013-03-24 04:10:54 +090053#if defined(OS_POSIX)
mseaborn@chromium.org056521b2012-05-30 23:26:13 +090054BASE_EXPORT int GetUrandomFD();
55#endif
56
mark@chromium.orgb93c0542008-09-30 07:18:01 +090057} // namespace base
58
erg@google.combf6ce9f2010-01-27 08:08:02 +090059#endif // BASE_RAND_UTIL_H_