rand_util.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_RAND_UTIL_H_
  5. #define BASE_RAND_UTIL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <string>
  10. #include "base/base_export.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. // Returns a random number in range [0, UINT64_MAX]. Thread-safe.
  14. BASE_EXPORT uint64_t RandUint64();
  15. // Returns a random number between min and max (inclusive). Thread-safe.
  16. BASE_EXPORT int RandInt(int min, int max);
  17. // Returns a random number in range [0, range). Thread-safe.
  18. BASE_EXPORT uint64_t RandGenerator(uint64_t range);
  19. // Returns a random double in range [0, 1). Thread-safe.
  20. BASE_EXPORT double RandDouble();
  21. // Given input |bits|, convert with maximum precision to a double in
  22. // the range [0, 1). Thread-safe.
  23. BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64_t bits);
  24. // Fills |output_length| bytes of |output| with random data. Thread-safe.
  25. //
  26. // Although implementations are required to use a cryptographically secure
  27. // random number source, code outside of base/ that relies on this should use
  28. // crypto::RandBytes instead to ensure the requirement is easily discoverable.
  29. BASE_EXPORT void RandBytes(void* output, size_t output_length);
  30. // Fills a string of length |length| with random data and returns it.
  31. // |length| should be nonzero. Thread-safe.
  32. //
  33. // Note that this is a variation of |RandBytes| with a different return type.
  34. // The returned string is likely not ASCII/UTF-8. Use with care.
  35. //
  36. // Although implementations are required to use a cryptographically secure
  37. // random number source, code outside of base/ that relies on this should use
  38. // crypto::RandBytes instead to ensure the requirement is easily discoverable.
  39. BASE_EXPORT std::string RandBytesAsString(size_t length);
  40. // An STL UniformRandomBitGenerator backed by RandUint64.
  41. // TODO(tzik): Consider replacing this with a faster implementation.
  42. class RandomBitGenerator {
  43. public:
  44. using result_type = uint64_t;
  45. static constexpr result_type min() { return 0; }
  46. static constexpr result_type max() { return UINT64_MAX; }
  47. result_type operator()() const { return RandUint64(); }
  48. RandomBitGenerator() = default;
  49. ~RandomBitGenerator() = default;
  50. };
  51. // Shuffles [first, last) randomly. Thread-safe.
  52. template <typename Itr>
  53. void RandomShuffle(Itr first, Itr last) {
  54. std::shuffle(first, last, RandomBitGenerator());
  55. }
  56. #if defined(OS_POSIX)
  57. BASE_EXPORT int GetUrandomFD();
  58. #endif
  59. } // namespace base
  60. #endif // BASE_RAND_UTIL_H_