1 /*
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 
11 #ifndef RTC_BASE_HELPERS_H_
12 #define RTC_BASE_HELPERS_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <string>
18 
19 #include "rtc_base/system/rtc_export.h"
20 
21 namespace rtc {
22 
23 // For testing, we can return predictable data.
24 void SetRandomTestMode(bool test);
25 
26 // Initializes the RNG, and seeds it with the specified entropy.
27 bool InitRandom(int seed);
28 bool InitRandom(const char* seed, size_t len);
29 
30 // Generates a (cryptographically) random string of the given length.
31 // We generate base64 values so that they will be printable.
32 RTC_EXPORT std::string CreateRandomString(size_t length);
33 
34 // Generates a (cryptographically) random string of the given length.
35 // We generate base64 values so that they will be printable.
36 // Return false if the random number generator failed.
37 RTC_EXPORT bool CreateRandomString(size_t length, std::string* str);
38 
39 // Generates a (cryptographically) random string of the given length,
40 // with characters from the given table. Return false if the random
41 // number generator failed.
42 // For ease of implementation, the function requires that the table
43 // size evenly divide 256; otherwise, it returns false.
44 RTC_EXPORT bool CreateRandomString(size_t length,
45                                    const std::string& table,
46                                    std::string* str);
47 
48 // Generates (cryptographically) random data of the given length.
49 // Return false if the random number generator failed.
50 bool CreateRandomData(size_t length, std::string* data);
51 
52 // Generates a (cryptographically) random UUID version 4 string.
53 std::string CreateRandomUuid();
54 
55 // Generates a random id.
56 uint32_t CreateRandomId();
57 
58 // Generates a 64 bit random id.
59 RTC_EXPORT uint64_t CreateRandomId64();
60 
61 // Generates a random id > 0.
62 uint32_t CreateRandomNonZeroId();
63 
64 // Generates a random double between 0.0 (inclusive) and 1.0 (exclusive).
65 double CreateRandomDouble();
66 
67 // Compute moving average with the given ratio between the previous average
68 // value and the current value.
69 double GetNextMovingAverage(double prev_average, double cur, double ratio);
70 
71 }  // namespace rtc
72 
73 #endif  // RTC_BASE_HELPERS_H_
74