From f65c1df0948d00c66eefa583efea6f578ba623b7 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Wed, 13 Jan 2021 13:54:19 +0000 Subject: Random: Add seeded PRNG --- Source/Core/Common/Random.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Source/Core/Common/Random.h') diff --git a/Source/Core/Common/Random.h b/Source/Core/Common/Random.h index 1f234f8ac3..789f62d9c1 100644 --- a/Source/Core/Common/Random.h +++ b/Source/Core/Common/Random.h @@ -5,12 +5,37 @@ #pragma once #include +#include #include #include "Common/CommonTypes.h" namespace Common::Random { +/// Cryptographically secure pseudo-random number generator, with explicit seed. +class PRNG final +{ +public: + explicit PRNG(u64 seed) : PRNG(&seed, sizeof(u64)) {} + PRNG(void* seed, std::size_t size); + ~PRNG(); + + void Generate(void* buffer, std::size_t size); + + template + T GenerateValue() + { + static_assert(std::is_arithmetic(), "T must be an arithmetic type in GenerateValue."); + T value; + Generate(&value, sizeof(value)); + return value; + } + +private: + struct Impl; + std::unique_ptr m_impl; +}; + /// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator. void Generate(void* buffer, std::size_t size); -- cgit v1.2.3