LLVM  8.0.1
RandomNumberGenerator.h
Go to the documentation of this file.
1 //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an abstraction for deterministic random number
11 // generation (RNG). Note that the current implementation is not
12 // cryptographically secure as it uses the C++11 <random> facilities.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
17 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
18 
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
21 #include <random>
22 #include <system_error>
23 
24 namespace llvm {
25 class StringRef;
26 
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.
34 
35  // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
36  // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
37  // This RNG is deterministically portable across C++11
38  // implementations.
39  using generator_type = std::mt19937_64;
40 
41 public:
42  using result_type = generator_type::result_type;
43 
44  /// Returns a random number in the range [0, Max).
46 
47  static constexpr result_type min() { return generator_type::min(); }
48  static constexpr result_type max() { return generator_type::max(); }
49 
50 private:
51  /// Seeds and salts the underlying RNG engine.
52  ///
53  /// This constructor should not be used directly. Instead use
54  /// Module::createRNG to create a new RNG salted with the Module ID.
56 
57  generator_type Generator;
58 
59  // Noncopyable.
60  RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
61  RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
62 
63  friend class Module;
64 };
65 
66 // Get random vector of specified size
67 std::error_code getRandomBytes(void *Buffer, size_t Size);
68 }
69 
70 #endif
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
std::error_code getRandomBytes(void *Buffer, size_t Size)
static constexpr result_type max()
generator_type::result_type result_type
A random number generator.
result_type operator()()
Returns a random number in the range [0, Max).
uint32_t Size
Definition: Profile.cpp:47
static constexpr result_type min()
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49