1 //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an abstraction for random number generation (RNG).
11 // Note that the current implementation is not cryptographically secure
12 // as it uses the C++11 <random> facilities.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
17 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
25 /// A random number generator.
26 /// Instances of this class should not be shared across threads.
27 class RandomNumberGenerator {
29 /// Seeds and salts the underlying RNG engine. The salt of type StringRef
30 /// is passed into the constructor. The seed can be set on the command
31 /// line via -rng-seed=<uint64>.
32 /// The reason for the salt is to ensure different random streams even if
33 /// the same seed is used for multiple invocations of the compiler.
34 /// A good salt value should add additional entropy and be constant across
35 /// different machines (i.e., no paths) to allow for reproducible builds.
36 /// An instance of this class can be retrieved from the current Module.
37 /// \see Module::getRNG
38 RandomNumberGenerator(StringRef Salt);
40 /// Returns a random number in the range [0, Max).
41 uint64_t next(uint64_t Max);
44 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
45 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
46 std::mt19937_64 Generator;
49 RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
50 RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;