LLVM API Documentation
00001 //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines an abstraction for random number generation (RNG). 00011 // Note that the current implementation is not cryptographically secure 00012 // as it uses the C++11 <random> facilities. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_ 00017 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_ 00018 00019 #include "llvm/ADT/StringRef.h" 00020 #include "llvm/Support/Compiler.h" 00021 #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows. 00022 #include <random> 00023 00024 namespace llvm { 00025 00026 /// A random number generator. 00027 /// Instances of this class should not be shared across threads. 00028 class RandomNumberGenerator { 00029 public: 00030 /// Seeds and salts the underlying RNG engine. The salt of type StringRef 00031 /// is passed into the constructor. The seed can be set on the command 00032 /// line via -rng-seed=<uint64>. 00033 /// The reason for the salt is to ensure different random streams even if 00034 /// the same seed is used for multiple invocations of the compiler. 00035 /// A good salt value should add additional entropy and be constant across 00036 /// different machines (i.e., no paths) to allow for reproducible builds. 00037 /// An instance of this class can be retrieved from the current Module. 00038 /// \see Module::getRNG 00039 RandomNumberGenerator(StringRef Salt); 00040 00041 /// Returns a random number in the range [0, Max). 00042 uint64_t next(uint64_t Max); 00043 00044 private: 00045 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000 00046 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine 00047 std::mt19937_64 Generator; 00048 00049 // Noncopyable. 00050 RandomNumberGenerator(const RandomNumberGenerator &other) 00051 LLVM_DELETED_FUNCTION; 00052 RandomNumberGenerator & 00053 operator=(const RandomNumberGenerator &other) LLVM_DELETED_FUNCTION; 00054 }; 00055 } 00056 00057 #endif