blob: f475797871cbf6bf3adb4edfc7fd94a361132276 [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; }
23 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
24 intptr_t operator()(intptr_t From, intptr_t To) {
25 assert(From < To);
26 intptr_t RangeSize = To - From + 1;
27 return operator()(RangeSize) + From;
28 }
29};
30
31} // namespace fuzzer
32
33#endif // LLVM_FUZZER_RANDOM_H