| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <type_traits> |
| 8 | |
| 9 | namespace Common::Random |
| 10 | { |
| 11 | /// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator. |
| 12 | void Generate(void* buffer, std::size_t size); |
| 13 | |
| 14 | /// Generates a random value of arithmetic type `T` |
| 15 | template <typename T> |
| 16 | T GenerateValue() |
| 17 | { |
| 18 | static_assert(std::is_arithmetic<T>(), "T must be an arithmetic type in GenerateValue."); |
| 19 | T value; |
| 20 | Generate(&value, sizeof(value)); |
| 21 | return value; |
| 22 | } |
| 23 | } // namespace Common::Random |