blob: 8a1aa3ef5fdc145fee0bf1b43bcaa578fc18dcf9 [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50:50 +00001//===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// fuzzer::Random
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_RANDOM_H
13#define LLVM_FUZZER_RANDOM_H
14
15#include <random>
16
17namespace fuzzer {
Kostya Serebryany6ac64c32017-02-07 22:37:34 +000018class Random : public std::mt19937 {
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000019 public:
Kostya Serebryany6ac64c32017-02-07 22:37:34 +000020 Random(unsigned int seed) : std::mt19937(seed) {}
21 result_type operator()() { return this->std::mt19937::operator()(); }
22 size_t Rand() { return this->operator()(); }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000023 size_t RandBool() { return Rand() % 2; }
24 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryanyc48c9312016-10-25 20:15:15 +000025 intptr_t operator()(intptr_t From, intptr_t To) {
26 assert(From < To);
27 intptr_t RangeSize = To - From + 1;
28 return operator()(RangeSize) + From;
29 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000030};
31
32} // namespace fuzzer
33
34#endif // LLVM_FUZZER_RANDOM_H