blob: 4dde7d84d88905905c7fe9834554380be732e454 [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 {
kccd6940342019-01-31 06:52:55 +000017class Random : public std::minstd_rand {
george.karpenkov29efa6d2017-08-21 23:25:50 +000018 public:
kccd6940342019-01-31 06:52:55 +000019 Random(unsigned int seed) : std::minstd_rand(seed) {}
20 result_type operator()() { return this->std::minstd_rand::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