1 // Copyright 2021 The Tint Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef FUZZERS_MERSENNE_TWISTER_ENGINE_H_ 16 #define FUZZERS_MERSENNE_TWISTER_ENGINE_H_ 17 18 #include <random> 19 20 #include "fuzzers/random_generator_engine.h" 21 22 namespace tint { 23 namespace fuzzers { 24 25 /// Standard MT based random number generation 26 class MersenneTwisterEngine : public RandomGeneratorEngine { 27 public: 28 /// @brief Initializes using provided seed 29 /// @param seed - seed value to use 30 explicit MersenneTwisterEngine(uint64_t seed); 31 ~MersenneTwisterEngine() override = default; 32 33 /// Generate random uint32_t value from uniform distribution. 34 /// @param lower - lower bound of integer generated 35 /// @param upper - upper bound of integer generated 36 /// @returns i, where lower <= i < upper 37 uint32_t RandomUInt32(uint32_t lower, uint32_t upper) override; 38 39 /// Get random uint64_t value from uniform distribution. 40 /// @param lower - lower bound of integer generated 41 /// @param upper - upper bound of integer generated 42 /// @returns i, where lower <= i < upper 43 uint64_t RandomUInt64(uint64_t lower, uint64_t upper) override; 44 45 /// Get N bytes of pseudo-random data 46 /// @param dest - memory location to store data 47 /// @param n - number of bytes of data to generate 48 void RandomNBytes(uint8_t* dest, size_t n) override; 49 50 private: 51 // Disallow copy & assign 52 MersenneTwisterEngine(const MersenneTwisterEngine&) = delete; 53 MersenneTwisterEngine& operator=(const MersenneTwisterEngine&) = delete; 54 55 std::mt19937_64 engine_; 56 57 }; // class MersenneTwisterEngine 58 59 } // namespace fuzzers 60 } // namespace tint 61 62 #endif // FUZZERS_MERSENNE_TWISTER_ENGINE_H_ 63