blob: af8e1a4eb71b760cd4fc1b942e6a905eaf3c1400 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// fuzzer::Random
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_FUZZER_RANDOM_H
12#define LLVM_FUZZER_RANDOM_H
13
14#include <random>
15
16namespace fuzzer {
jmorse79cac902019-01-31 12:44:14 +000017class Random : public std::mt19937 {
george.karpenkov29efa6d2017-08-21 23:25:50 +000018 public:
jmorse79cac902019-01-31 12:44:14 +000019 Random(unsigned int seed) : std::mt19937(seed) {}
20 result_type operator()() { return this->std::mt19937::operator()(); }
george.karpenkov29efa6d2017-08-21 23:25:50 +000021 size_t Rand() { return this->operator()(); }
22 size_t RandBool() { return Rand() % 2; }
kcc243006d2019-02-12 00:12:33 +000023 size_t SkewTowardsLast(size_t n) {
24 size_t T = this->operator()(n * n);
25 size_t Res = sqrt(T);
26 return Res;
27 }
george.karpenkov29efa6d2017-08-21 23:25:50 +000028 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
29 intptr_t operator()(intptr_t From, intptr_t To) {
30 assert(From < To);
31 intptr_t RangeSize = To - From + 1;
32 return operator()(RangeSize) + From;
33 }
34};
35
36} // namespace fuzzer
37
38#endif // LLVM_FUZZER_RANDOM_H