From fb9d00b87f47a043b1f4a314be6607085947d3c8 Mon Sep 17 00:00:00 2001 From: Aetias Date: Sun, 20 Oct 2024 10:39:09 +0200 Subject: Fix build; add RNG code --- include/System/Random.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 include/System/Random.hpp (limited to 'include/System/Random.hpp') diff --git a/include/System/Random.hpp b/include/System/Random.hpp new file mode 100644 index 00000000..4506466d --- /dev/null +++ b/include/System/Random.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "global.h" +#include "types.h" + +struct Random { + /* 00 */ u64 mRandomValue; + /* 08 */ u64 mFactor; + /* 10 */ u64 mAddend; + /* 18 */ + + /** + * Generate a random number from 0 (inclusive) to `max` (exclusive) + */ + inline u32 Next(u32 max) { + mRandomValue = mAddend + mFactor * mRandomValue; + u64 result = (mRandomValue >> 32) * max; + return result >> 32; + } +}; + +extern Random *gRandom; -- cgit v1.2.3