blob: c214f5212fbe04e3de14d9e59622f7599179cf5a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef RTC_BASE_HELPERS_H_
12#define RTC_BASE_HELPERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3316
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2618
Ali Tofigh7fa90572022-03-17 14:47:4919#include "absl/strings/string_view.h"
Mirko Bonadei35214fc2019-09-23 12:54:2820#include "rtc_base/system/rtc_export.h"
21
Henrik Kjellanderec78f1c2017-06-29 05:52:5022namespace rtc {
23
24// For testing, we can return predictable data.
25void SetRandomTestMode(bool test);
26
27// Initializes the RNG, and seeds it with the specified entropy.
28bool InitRandom(int seed);
29bool InitRandom(const char* seed, size_t len);
30
31// Generates a (cryptographically) random string of the given length.
32// We generate base64 values so that they will be printable.
Mirko Bonadei35214fc2019-09-23 12:54:2833RTC_EXPORT std::string CreateRandomString(size_t length);
Henrik Kjellanderec78f1c2017-06-29 05:52:5034
35// Generates a (cryptographically) random string of the given length.
36// We generate base64 values so that they will be printable.
37// Return false if the random number generator failed.
Mirko Bonadei35214fc2019-09-23 12:54:2838RTC_EXPORT bool CreateRandomString(size_t length, std::string* str);
Henrik Kjellanderec78f1c2017-06-29 05:52:5039
40// Generates a (cryptographically) random string of the given length,
41// with characters from the given table. Return false if the random
42// number generator failed.
43// For ease of implementation, the function requires that the table
44// size evenly divide 256; otherwise, it returns false.
Mirko Bonadei35214fc2019-09-23 12:54:2845RTC_EXPORT bool CreateRandomString(size_t length,
Ali Tofigh7fa90572022-03-17 14:47:4946 absl::string_view table,
Mirko Bonadei35214fc2019-09-23 12:54:2847 std::string* str);
Henrik Kjellanderec78f1c2017-06-29 05:52:5048
49// Generates (cryptographically) random data of the given length.
50// Return false if the random number generator failed.
51bool CreateRandomData(size_t length, std::string* data);
52
53// Generates a (cryptographically) random UUID version 4 string.
54std::string CreateRandomUuid();
55
56// Generates a random id.
57uint32_t CreateRandomId();
58
59// Generates a 64 bit random id.
Mirko Bonadei35214fc2019-09-23 12:54:2860RTC_EXPORT uint64_t CreateRandomId64();
Henrik Kjellanderec78f1c2017-06-29 05:52:5061
62// Generates a random id > 0.
63uint32_t CreateRandomNonZeroId();
64
65// Generates a random double between 0.0 (inclusive) and 1.0 (exclusive).
66double CreateRandomDouble();
67
Qingsi Wang72a43a12018-02-21 00:03:1868// Compute moving average with the given ratio between the previous average
69// value and the current value.
70double GetNextMovingAverage(double prev_average, double cur, double ratio);
71
Henrik Kjellanderec78f1c2017-06-29 05:52:5072} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2673
Mirko Bonadei92ea95e2017-09-15 04:47:3174#endif // RTC_BASE_HELPERS_H_