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()(); } 210b57cec5SDimitry Andric template <typename T> Rand()220b57cec5SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type Rand() { 230b57cec5SDimitry Andric return static_cast<T>(this->operator()()); 240b57cec5SDimitry Andric } RandBool()250b57cec5SDimitry 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); 280b57cec5SDimitry Andric size_t Res = static_cast<size_t>(sqrt(T)); 290b57cec5SDimitry Andric return Res; 300b57cec5SDimitry Andric } 310b57cec5SDimitry Andric template <typename T> operator()320b57cec5SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type operator()(T n) { 330b57cec5SDimitry Andric return n ? Rand<T>() % n : 0; 340b57cec5SDimitry Andric } 350b57cec5SDimitry Andric template <typename T> 360b57cec5SDimitry Andric typename std::enable_if<std::is_integral<T>::value, T>::type operator()370b57cec5SDimitry Andric operator()(T From, T To) { 380b57cec5SDimitry Andric assert(From < To); 39 auto RangeSize = static_cast<unsigned long long>(To) - 40 static_cast<unsigned long long>(From) + 1; 41 return static_cast<T>(this->operator()(RangeSize) + From); 42 } 43 }; 44 45 } // namespace fuzzer 46 47 #endif // LLVM_FUZZER_RANDOM_H 48