FuzzerRandom.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. #ifndef LLVM_FUZZER_RANDOM_H
  11. #define LLVM_FUZZER_RANDOM_H
  12. #include <random>
  13. namespace fuzzer {
  14. class Random : public std::minstd_rand {
  15. public:
  16. Random(unsigned int seed) : std::minstd_rand(seed) {}
  17. result_type operator()() { return this->std::minstd_rand::operator()(); }
  18. size_t Rand() { return this->operator()(); }
  19. size_t RandBool() { return Rand() % 2; }
  20. size_t SkewTowardsLast(size_t n) {
  21. size_t T = this->operator()(n * n);
  22. size_t Res = sqrt(T);
  23. return Res;
  24. }
  25. size_t operator()(size_t n) { return n ? Rand() % n : 0; }
  26. intptr_t operator()(intptr_t From, intptr_t To) {
  27. assert(From < To);
  28. intptr_t RangeSize = To - From + 1;
  29. return operator()(RangeSize) + From;
  30. }
  31. };
  32. } // namespace fuzzer
  33. #endif // LLVM_FUZZER_RANDOM_H