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 "absl/strings/string_view.h" 20 #include "rtc_base/system/rtc_export.h" 21 22 namespace rtc { 23 24 // For testing, we can return predictable data. 25 void SetRandomTestMode(bool test); 26 27 // Initializes the RNG, and seeds it with the specified entropy. 28 bool InitRandom(int seed); 29 bool 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. 33 RTC_EXPORT std::string CreateRandomString(size_t length); 34 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. 38 RTC_EXPORT bool CreateRandomString(size_t length, std::string* str); 39 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. 45 RTC_EXPORT bool CreateRandomString(size_t length, 46 absl::string_view table, 47 std::string* str); 48 49 // Generates (cryptographically) random data of the given length. 50 // Return false if the random number generator failed. 51 bool CreateRandomData(size_t length, std::string* data); 52 53 // Generates a (cryptographically) random UUID version 4 string. 54 std::string CreateRandomUuid(); 55 56 // Generates a random id. 57 uint32_t CreateRandomId(); 58 59 // Generates a 64 bit random id. 60 RTC_EXPORT uint64_t CreateRandomId64(); 61 62 // Generates a random id > 0. 63 uint32_t CreateRandomNonZeroId(); 64 65 // Generates a random double between 0.0 (inclusive) and 1.0 (exclusive). 66 double CreateRandomDouble(); 67 68 // Compute moving average with the given ratio between the previous average 69 // value and the current value. 70 double GetNextMovingAverage(double prev_average, double cur, double ratio); 71 72 } // namespace rtc 73 74 #endif // RTC_BASE_HELPERS_H_ 75