RandomNumberGenerator.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines an abstraction for deterministic random number
  15. // generation (RNG). Note that the current implementation is not
  16. // cryptographically secure as it uses the C++11 <random> facilities.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
  20. #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
  21. #include "llvm/Support/Compiler.h"
  22. #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
  23. #include <random>
  24. #include <system_error>
  25. namespace llvm {
  26. class StringRef;
  27. /// A random number generator.
  28. ///
  29. /// Instances of this class should not be shared across threads. The
  30. /// seed should be set by passing the -rng-seed=<uint64> option. Use
  31. /// Module::createRNG to create a new RNG instance for use with that
  32. /// module.
  33. class RandomNumberGenerator {
  34. // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
  35. // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
  36. // This RNG is deterministically portable across C++11
  37. // implementations.
  38. using generator_type = std::mt19937_64;
  39. public:
  40. using result_type = generator_type::result_type;
  41. /// Returns a random number in the range [0, Max).
  42. result_type operator()();
  43. static constexpr result_type min() { return generator_type::min(); }
  44. static constexpr result_type max() { return generator_type::max(); }
  45. private:
  46. /// Seeds and salts the underlying RNG engine.
  47. ///
  48. /// This constructor should not be used directly. Instead use
  49. /// Module::createRNG to create a new RNG salted with the Module ID.
  50. RandomNumberGenerator(StringRef Salt);
  51. generator_type Generator;
  52. // Noncopyable.
  53. RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
  54. RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
  55. friend class Module;
  56. };
  57. // Get random vector of specified size
  58. std::error_code getRandomBytes(void *Buffer, size_t Size);
  59. }
  60. #endif
  61. #ifdef __GNUC__
  62. #pragma GCC diagnostic pop
  63. #endif