rand_int.h 734 B

12345678910111213141516171819202122232425262728
  1. /* This file is part of the dynarmic project.
  2. * Copyright (c) 2020 MerryMage
  3. * SPDX-License-Identifier: 0BSD
  4. */
  5. #pragma once
  6. #include <random>
  7. #include <type_traits>
  8. namespace detail {
  9. inline std::mt19937 g_rand_int_generator = [] {
  10. std::random_device rd;
  11. std::mt19937 mt{rd()};
  12. return mt;
  13. }();
  14. } // namespace detail
  15. template<typename T>
  16. T RandInt(T min, T max) {
  17. static_assert(std::is_integral_v<T>, "T must be an integral type.");
  18. static_assert(!std::is_same_v<T, signed char> && !std::is_same_v<T, unsigned char>,
  19. "Using char with uniform_int_distribution is undefined behavior.");
  20. std::uniform_int_distribution<T> rand(min, max);
  21. return rand(detail::g_rand_int_generator);
  22. }