Skip to content

Commit

Permalink
Removed SDL_rand_r()
Browse files Browse the repository at this point in the history
  • Loading branch information
JKaniarz authored and slouken committed Jun 19, 2024
1 parent 86b06f7 commit 16e69cb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 51 deletions.
28 changes: 1 addition & 27 deletions include/SDL3/SDL_stdinc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 0 additions & 1 deletion src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 14 additions & 22 deletions src/stdlib/SDL_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

0 comments on commit 16e69cb

Please sign in to comment.