summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Random.h
diff options
context:
space:
mode:
authorMerryMage <MerryMage@users.noreply.github.com>2021-01-13 13:54:19 +0000
committerMerryMage <MerryMage@users.noreply.github.com>2021-01-31 13:16:45 +0000
commitf65c1df0948d00c66eefa583efea6f578ba623b7 (patch)
treedaf86494e9eb2b3e5eb691fd6e0074cb24a87f32 /Source/Core/Common/Random.h
parentb22073ef595a5c186eccb54c83ccbe61cf33019b (diff)
Random: Add seeded PRNG
Diffstat (limited to 'Source/Core/Common/Random.h')
-rw-r--r--Source/Core/Common/Random.h25
1 files changed, 25 insertions, 0 deletions
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 <cstddef>
+#include <memory>
#include <type_traits>
#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 <typename T>
+ T GenerateValue()
+ {
+ static_assert(std::is_arithmetic<T>(), "T must be an arithmetic type in GenerateValue.");
+ T value;
+ Generate(&value, sizeof(value));
+ return value;
+ }
+
+private:
+ struct Impl;
+ std::unique_ptr<Impl> m_impl;
+};
+
/// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator.
void Generate(void* buffer, std::size_t size);