blob: 45e42832237a34b3098ea33ada2a968ab99de6fd [file] [log] [blame]
[email protected]ead8c1fa2012-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
tzik5de2157f2018-05-08 03:42:4711#include <algorithm>
[email protected]29548d82011-04-29 21:03:5412#include <string>
13
[email protected]0bea7252011-08-05 15:34:0014#include "base/base_export.h"
avibf0746c2015-12-09 19:53:1415#include "build/build_config.h"
[email protected]05f9b682008-09-29 22:18:0116
17namespace base {
18
avibf0746c2015-12-09 19:53:1419// Returns a random number in range [0, UINT64_MAX]. Thread-safe.
20BASE_EXPORT uint64_t RandUint64();
[email protected]05f9b682008-09-29 22:18:0121
22// Returns a random number between min and max (inclusive). Thread-safe.
[email protected]0bea7252011-08-05 15:34:0023BASE_EXPORT int RandInt(int min, int max);
[email protected]05f9b682008-09-29 22:18:0124
[email protected]0173b962011-08-24 19:58:3625// Returns a random number in range [0, range). Thread-safe.
avibf0746c2015-12-09 19:53:1426BASE_EXPORT uint64_t RandGenerator(uint64_t 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.
avibf0746c2015-12-09 19:53:1433BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64_t bits);
[email protected]edafd4c2011-05-10 17:18:5334
John Mellorafab972d2017-09-26 16:28:1935// Fills |output_length| bytes of |output| with random data. Thread-safe.
[email protected]9b205782012-08-02 20:22:2536//
John Mellorafab972d2017-09-26 16:28:1937// Although implementations are required to use a cryptographically secure
38// random number source, code outside of base/ that relies on this should use
39// crypto::RandBytes instead to ensure the requirement is easily discoverable.
[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]3818dd02014-05-13 05:56:1942// Fills a string of length |length| with random data and returns it.
John Mellorafab972d2017-09-26 16:28:1943// |length| should be nonzero. Thread-safe.
[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]3818dd02014-05-13 05:56:1946// The returned string is likely not ASCII/UTF-8. Use with care.
[email protected]9b205782012-08-02 20:22:2547//
John Mellorafab972d2017-09-26 16:28:1948// Although implementations are required to use a cryptographically secure
49// random number source, code outside of base/ that relies on this should use
50// crypto::RandBytes instead to ensure the requirement is easily discoverable.
[email protected]0bea7252011-08-05 15:34:0051BASE_EXPORT std::string RandBytesAsString(size_t length);
[email protected]29548d82011-04-29 21:03:5452
tzik5de2157f2018-05-08 03:42:4753// An STL UniformRandomBitGenerator backed by RandUint64.
54// TODO(tzik): Consider replacing this with a faster implementation.
55class RandomBitGenerator {
56 public:
57 using result_type = uint64_t;
58 static constexpr result_type min() { return 0; }
59 static constexpr result_type max() { return UINT64_MAX; }
60 result_type operator()() const { return RandUint64(); }
61
62 RandomBitGenerator() = default;
63 ~RandomBitGenerator() = default;
64};
65
66// Shuffles [first, last) randomly. Thread-safe.
67template <typename Itr>
68void RandomShuffle(Itr first, Itr last) {
69 std::shuffle(first, last, RandomBitGenerator());
70}
71
Wez35539132018-07-17 11:26:0572#if defined(OS_POSIX)
[email protected]ead8c1fa2012-05-30 14:26:1373BASE_EXPORT int GetUrandomFD();
74#endif
75
[email protected]05f9b682008-09-29 22:18:0176} // namespace base
77
[email protected]2fdc86a2010-01-26 23:08:0278#endif // BASE_RAND_UTIL_H_