1*0b57cec5SDimitry Andric //===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // fuzzer::Random 9*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 10*0b57cec5SDimitry Andric 11*0b57cec5SDimitry Andric #ifndef LLVM_FUZZER_RANDOM_H 12*0b57cec5SDimitry Andric #define LLVM_FUZZER_RANDOM_H 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include <random> 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric namespace fuzzer { 17*0b57cec5SDimitry Andric class Random : public std::minstd_rand { 18*0b57cec5SDimitry Andric public: 19*0b57cec5SDimitry Andric Random(unsigned int seed) : std::minstd_rand(seed) {} 20*0b57cec5SDimitry Andric result_type operator()() { return this->std::minstd_rand::operator()(); } 21*0b57cec5SDimitry Andric size_t Rand() { return this->operator()(); } 22*0b57cec5SDimitry Andric size_t RandBool() { return Rand() % 2; } 23*0b57cec5SDimitry Andric size_t SkewTowardsLast(size_t n) { 24*0b57cec5SDimitry Andric size_t T = this->operator()(n * n); 25*0b57cec5SDimitry Andric size_t Res = sqrt(T); 26*0b57cec5SDimitry Andric return Res; 27*0b57cec5SDimitry Andric } 28*0b57cec5SDimitry Andric size_t operator()(size_t n) { return n ? Rand() % n : 0; } 29*0b57cec5SDimitry Andric intptr_t operator()(intptr_t From, intptr_t To) { 30*0b57cec5SDimitry Andric assert(From < To); 31*0b57cec5SDimitry Andric intptr_t RangeSize = To - From + 1; 32*0b57cec5SDimitry Andric return operator()(RangeSize) + From; 33*0b57cec5SDimitry Andric } 34*0b57cec5SDimitry Andric }; 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric } // namespace fuzzer 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric #endif // LLVM_FUZZER_RANDOM_H 39