10b57cec5SDimitry Andric //===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // fuzzer::Random 90b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 100b57cec5SDimitry Andric 110b57cec5SDimitry Andric #ifndef LLVM_FUZZER_RANDOM_H 120b57cec5SDimitry Andric #define LLVM_FUZZER_RANDOM_H 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include <random> 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric namespace fuzzer { 170b57cec5SDimitry Andric class Random : public std::minstd_rand { 180b57cec5SDimitry Andric public: Random(unsigned int seed)190b57cec5SDimitry Andric Random(unsigned int seed) : std::minstd_rand(seed) {} operator()200b57cec5SDimitry Andric result_type operator()() { return this->std::minstd_rand::operator()(); } 21*fe6060f1SDimitry Andric template <typename T> Rand()22*fe6060f1SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type Rand() { 23*fe6060f1SDimitry Andric return static_cast<T>(this->operator()()); 24*fe6060f1SDimitry Andric } RandBool()25*fe6060f1SDimitry Andric size_t RandBool() { return this->operator()() % 2; } SkewTowardsLast(size_t n)260b57cec5SDimitry Andric size_t SkewTowardsLast(size_t n) { 270b57cec5SDimitry Andric size_t T = this->operator()(n * n); 28*fe6060f1SDimitry Andric size_t Res = static_cast<size_t>(sqrt(T)); 290b57cec5SDimitry Andric return Res; 300b57cec5SDimitry Andric } 31*fe6060f1SDimitry Andric template <typename T> operator()32*fe6060f1SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type operator()(T n) { 33*fe6060f1SDimitry Andric return n ? Rand<T>() % n : 0; 34*fe6060f1SDimitry Andric } 35*fe6060f1SDimitry Andric template <typename T> 36*fe6060f1SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type operator()37*fe6060f1SDimitry Andric operator()(T From, T To) { 380b57cec5SDimitry Andric assert(From < To); 39*fe6060f1SDimitry Andric auto RangeSize = static_cast<unsigned long long>(To) - 40*fe6060f1SDimitry Andric static_cast<unsigned long long>(From) + 1; 41*fe6060f1SDimitry Andric return static_cast<T>(this->operator()(RangeSize) + From); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric }; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric } // namespace fuzzer 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric #endif // LLVM_FUZZER_RANDOM_H 48