1 //===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_RANDOM_H
12 #define LLVM_FUZZER_RANDOM_H
17 class Random
: public std::minstd_rand
{
19 Random(unsigned int seed
) : std::minstd_rand(seed
) {}
20 result_type
operator()() { return this->std::minstd_rand::operator()(); }
22 typename
std::enable_if
<std::is_integral
<T
>::value
, T
>::type
Rand() {
23 return static_cast<T
>(this->operator()());
25 size_t RandBool() { return this->operator()() % 2; }
26 size_t SkewTowardsLast(size_t n
) {
27 size_t T
= this->operator()(n
* n
);
28 size_t Res
= static_cast<size_t>(sqrt(T
));
32 typename
std::enable_if
<std::is_integral
<T
>::value
, T
>::type
operator()(T n
) {
33 return n
? Rand
<T
>() % n
: 0;
36 typename
std::enable_if
<std::is_integral
<T
>::value
, T
>::type
37 operator()(T From
, T 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
);
47 #endif // LLVM_FUZZER_RANDOM_H