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 deterministic random number
11 // generation (RNG). Note that the current implementation is not
12 // cryptographically secure 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/Compiler.h"
21 #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
26 /// A random number generator.
28 /// Instances of this class should not be shared across threads. The
29 /// seed should be set by passing the -rng-seed=<uint64> option. Use
30 /// Module::createRNG to create a new RNG instance for use with that
32 class RandomNumberGenerator {
34 /// Returns a random number in the range [0, Max).
35 uint_fast64_t operator()();
38 /// Seeds and salts the underlying RNG engine.
40 /// This constructor should not be used directly. Instead use
41 /// Module::createRNG to create a new RNG salted with the Module ID.
42 RandomNumberGenerator(StringRef Salt);
44 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
45 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
46 // This RNG is deterministically portable across C++11
48 std::mt19937_64 Generator;
51 RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
52 RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;