xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerRandom.h (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 //===- FuzzerRandom.h - Internal header for the Fuzzer ----------*- C++ -* ===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 // fuzzer::Random
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_FUZZER_RANDOM_H
12 #define LLVM_FUZZER_RANDOM_H
13 
14 #include <random>
15 
16 namespace fuzzer {
17 class Random : public std::minstd_rand {
18  public:
19   Random(unsigned int seed) : std::minstd_rand(seed) {}
20   result_type operator()() { return this->std::minstd_rand::operator()(); }
21   size_t Rand() { return this->operator()(); }
22   size_t RandBool() { return Rand() % 2; }
23   size_t SkewTowardsLast(size_t n) {
24     size_t T = this->operator()(n * n);
25     size_t Res = sqrt(T);
26     return Res;
27   }
28   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
39