Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- NaClRandNumGen.h - random number generator ---------------*- 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 a random number generator API for 64-bit unsigned | |
| 11 // values, and a corresponding default implementation. | |
| 12 // | |
| 13 // *** WARNING *** One should assume that random number generators are not | |
| 14 // thread safe. | |
|
jvoung (off chromium)
2015/06/01 17:26:35
same
Karl
2015/06/01 22:40:54
Done.
| |
| 15 | |
| 16 #ifndef LLVM_BITCODE_NACL_NACLRANDNUMGEN_H | |
| 17 #define LLVM_BITCODE_NACL_NACLRANDNUMGEN_H | |
| 18 | |
| 19 #include <random> | |
| 20 | |
| 21 namespace naclfuzz { | |
| 22 | |
| 23 /// Defines API for a random number generator to use with fuzzing. | |
| 24 class RandomNumberGenerator { | |
| 25 RandomNumberGenerator(const RandomNumberGenerator&) = delete; | |
| 26 void operator=(const RandomNumberGenerator&) = delete; | |
| 27 public: | |
| 28 virtual ~RandomNumberGenerator(); | |
| 29 /// Returns a random number. | |
| 30 virtual uint64_t operator()() = 0; | |
| 31 // Returns a random value in [0..Limit) | |
| 32 uint64_t chooseInRange(uint64_t Limit) { | |
| 33 return (*this)() % Limit; | |
| 34 } | |
| 35 protected: | |
| 36 RandomNumberGenerator() {} | |
| 37 }; | |
| 38 | |
| 39 /// Defines a random number generator based on C++ generator std::mt19937_64. | |
| 40 class DefaultRandomNumberGenerator : public RandomNumberGenerator { | |
| 41 DefaultRandomNumberGenerator(const DefaultRandomNumberGenerator&) = delete; | |
| 42 void operator=(const DefaultRandomNumberGenerator&) = delete; | |
| 43 public: | |
| 44 DefaultRandomNumberGenerator(std::string Seed); | |
|
jvoung (off chromium)
2015/06/01 17:26:35
StringRef or const std::string &
Karl
2015/06/01 22:40:54
Done.
| |
| 45 uint64_t operator()() final; | |
| 46 virtual ~DefaultRandomNumberGenerator() {} | |
|
jvoung (off chromium)
2015/06/01 17:26:35
override instead of virtual?
Karl
2015/06/01 22:40:54
Changed to final.
| |
| 47 // Sets random number seed by salting seed of constructor with Salt. | |
| 48 void setSeed(uint64_t Salt); | |
| 49 private: | |
| 50 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000 | |
| 51 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine | |
| 52 // This RNG is deterministically portable across C++11 | |
| 53 // implementations. | |
| 54 std::mt19937_64 Generator; | |
| 55 // Seed for the random number generator. | |
| 56 std::string Seed; | |
| 57 }; | |
| 58 | |
| 59 } // end of namespace naclfuzz | |
| 60 | |
| 61 #endif // LLVM_BITCODE_NACL_NACLRANDNUMGEN_H | |
| OLD | NEW |