From 505131a268f607f85ba46e0d36a5237244f224ba Mon Sep 17 00:00:00 2001 From: Tommi Sinivuo <600815+TommiSinivuo@users.noreply.github.com> Date: Fri, 17 Sep 2021 09:29:15 +0300 Subject: [PATCH] Add SetRandomSeed(unsigned int seed) function Specifying a fixed seed for the random number generator is often used in games for various reasons. By adding an api function for seeding the random number generator we solve two different problems regarding the seeding: 1) The underlying RNG implementation does not leak to client code (as would be the case if we called srand directly from the client code) 2) Seeding the RNG would be simple from other programming languages (especially in cases where calling libc functions is non-trivial) --- src/core.c | 6 ++++++ src/raylib.h | 1 + 2 files changed, 7 insertions(+) diff --git a/src/core.c b/src/core.c index 711aa889b..0cace3a6f 100644 --- a/src/core.c +++ b/src/core.c @@ -2669,6 +2669,12 @@ int GetRandomValue(int min, int max) return (rand()%(abs(max - min) + 1) + min); } +// Set the seed for the random number generator +void SetRandomSeed(unsigned int seed) +{ + srand(seed); +} + // Check if the file exists bool FileExists(const char *fileName) { diff --git a/src/raylib.h b/src/raylib.h index 3adc5c520..865d9d458 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1009,6 +1009,7 @@ RLAPI double GetTime(void); // Get elapsed // Misc. functions RLAPI int GetRandomValue(int min, int max); // Get a random value between min and max (both included) +RLAPI void SetRandomSeed(unsigned int seed); // Set the seed for the random number generator RLAPI void TakeScreenshot(const char *fileName); // Takes a screenshot of current screen (filename extension defines format) RLAPI void SetConfigFlags(unsigned int flags); // Setup init configuration flags (view FLAGS)