rand_util.h 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_IPC_FUZZER_MUTATE_RAND_UTIL_H_
  5. #define TOOLS_IPC_FUZZER_MUTATE_RAND_UTIL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <random>
  9. namespace ipc_fuzzer {
  10. extern std::mt19937* g_mersenne_twister;
  11. void InitRand();
  12. inline uint32_t RandU32() {
  13. return (*g_mersenne_twister)();
  14. }
  15. inline uint64_t RandU64() {
  16. return (static_cast<uint64_t>(RandU32()) << 32) | RandU32();
  17. }
  18. inline double RandDouble() {
  19. uint64_t rand_u64 = RandU64();
  20. return *reinterpret_cast<double*>(&rand_u64);
  21. }
  22. inline uint32_t RandInRange(uint32_t range) {
  23. return RandU32() % range;
  24. }
  25. inline bool RandEvent(uint32_t frequency) {
  26. return RandInRange(frequency) == 0;
  27. }
  28. inline size_t RandElementCount() {
  29. return RandU32() % 10;
  30. }
  31. } // namespace ipc_fuzzer
  32. #endif // TOOLS_IPC_FUZZER_MUTATE_RAND_UTIL_H_