helpers.h 2.5 KB

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