diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h index bab9c637b1..203c182f81 100644 --- a/include/SDL3/SDL_stdinc.h +++ b/include/SDL3/SDL_stdinc.h @@ -1292,42 +1292,16 @@ extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed); * * \returns a random value in the range of [0-SDL_MAX_UINT32]. * - * \threadsafety All calls should be made from a single thread, use - * SDL_rand_r() when using multiple threads. + * \threadsafety All calls should be made from a single thread * * \since This function is available since SDL 3.0.0. * - * \sa SDL_rand_r * \sa SDL_srand * \sa SDL_rand_n * \sa SDL_rand_float */ extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void); -/** - * Get 32 pseudo-random bits. - * - * There are no guarantees as to the quality of the random sequence produced, - * and this should not be used for security (cryptography, passwords) or where - * money is on the line (loot-boxes, casinos). There are many random number - * libraries available with different characteristics and you should pick one of - * those to meet any serious needs. - * - * \param state a pointer to a 64-bit seed value that will be updated with - * each call to SDL_rand_r(). If the value of the seed is 0, it - * will be initialized with SDL_GetPerformanceCounter(). - * \returns a random value in the range of [0-SDL_MAX_UINT32], or 0 if state - * is NULL. - * - * \threadsafety This can be called from any thread, however each thread - * should pass its own state pointer. - * - * \since This function is available since SDL 3.0.0. - * - * \sa SDL_rand - */ -extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_r(Uint64 *state); - /** * Generates a pseudo-random number less than n * diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 5f779db4d3..40d1e33ae2 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -983,7 +983,6 @@ #define SDL_rand SDL_rand_REAL #define SDL_rand_float SDL_rand_float_REAL #define SDL_rand_n SDL_rand_n_REAL -#define SDL_rand_r SDL_rand_r_REAL #define SDL_realloc SDL_realloc_REAL #define SDL_round SDL_round_REAL #define SDL_roundf SDL_roundf_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 264ade7563..bc1c24dd7a 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -992,7 +992,6 @@ SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallba SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return) SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return) SDL_DYNAPI_PROC(Uint32,SDL_rand_n,(Uint32 a),(a),return) -SDL_DYNAPI_PROC(Uint32,SDL_rand_r,(Uint64 *a),(a),return) SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return) SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return) diff --git a/src/stdlib/SDL_random.c b/src/stdlib/SDL_random.c index 2f3cac502e..05005db3b0 100644 --- a/src/stdlib/SDL_random.c +++ b/src/stdlib/SDL_random.c @@ -39,26 +39,6 @@ Uint32 SDL_rand(void) if(!SDL_rand_initialized) { SDL_srand(0); } - return SDL_rand_r(&SDL_rand_state); -} - -Uint32 SDL_rand_n(Uint32 n) -{ - // On 32-bit arch, the compiler will optimize to a single 32-bit multiply - Uint64 val = (Uint64)SDL_rand() * n; - return (Uint32)(val >> 32); -} - -float SDL_rand_float(void) -{ - return (SDL_rand() >> (32-24)) * 0x1p-24f; -} - -Uint32 SDL_rand_r(Uint64 *state) -{ - if (!state) { - return 0; - } // The C and A parameters of this LCG have been chosen based on hundreds // of core-hours of testing with PractRand and TestU01's Crush. @@ -75,8 +55,20 @@ Uint32 SDL_rand_r(Uint64 *state) // Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030 // https://arxiv.org/abs/2001.05304v2 - *state = *state * 0xff1cd035ul + 0x05; + SDL_rand_state = SDL_rand_state * 0xff1cd035ul + 0x05; // Only return top 32 bits because they have a longer period - return (Uint32)(*state >> 32); + return (Uint32)(SDL_rand_state >> 32); +} + +Uint32 SDL_rand_n(Uint32 n) +{ + // On 32-bit arch, the compiler will optimize to a single 32-bit multiply + Uint64 val = (Uint64)SDL_rand() * n; + return (Uint32)(val >> 32); +} + +float SDL_rand_float(void) +{ + return (SDL_rand() >> (32-24)) * 0x1p-24f; }