blob: d3b09cfd48fb5bd3e5afd93bd6f854a11ace0ef7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 06:47:31 +020011#ifndef RTC_BASE_HELPERS_H_
12#define RTC_BASE_HELPERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016namespace rtc {
17
18// For testing, we can return predictable data.
19void SetRandomTestMode(bool test);
20
21// Initializes the RNG, and seeds it with the specified entropy.
22bool InitRandom(int seed);
23bool InitRandom(const char* seed, size_t len);
24
25// Generates a (cryptographically) random string of the given length.
26// We generate base64 values so that they will be printable.
27std::string CreateRandomString(size_t length);
28
29// Generates a (cryptographically) random string of the given length.
30// We generate base64 values so that they will be printable.
31// Return false if the random number generator failed.
32bool CreateRandomString(size_t length, std::string* str);
33
34// Generates a (cryptographically) random string of the given length,
35// with characters from the given table. Return false if the random
36// number generator failed.
37// For ease of implementation, the function requires that the table
38// size evenly divide 256; otherwise, it returns false.
Yves Gerey665174f2018-06-19 15:03:05 +020039bool CreateRandomString(size_t length,
40 const std::string& table,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 std::string* str);
42
43// Generates (cryptographically) random data of the given length.
44// Return false if the random number generator failed.
45bool CreateRandomData(size_t length, std::string* data);
46
47// Generates a (cryptographically) random UUID version 4 string.
48std::string CreateRandomUuid();
49
50// Generates a random id.
51uint32_t CreateRandomId();
52
53// Generates a 64 bit random id.
54uint64_t CreateRandomId64();
55
56// Generates a random id > 0.
57uint32_t CreateRandomNonZeroId();
58
59// Generates a random double between 0.0 (inclusive) and 1.0 (exclusive).
60double CreateRandomDouble();
61
Qingsi Wang72a43a12018-02-20 16:03:18 -080062// Compute moving average with the given ratio between the previous average
63// value and the current value.
64double GetNextMovingAverage(double prev_average, double cur, double ratio);
65
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020068#endif // RTC_BASE_HELPERS_H_