Skip to content

Commit 16e69cb

Browse files
JKaniarzslouken
authored andcommitted
Removed SDL_rand_r()
1 parent 86b06f7 commit 16e69cb

File tree

4 files changed

+15
-51
lines changed

4 files changed

+15
-51
lines changed

include/SDL3/SDL_stdinc.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,42 +1292,16 @@ extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
12921292
*
12931293
* \returns a random value in the range of [0-SDL_MAX_UINT32].
12941294
*
1295-
* \threadsafety All calls should be made from a single thread, use
1296-
* SDL_rand_r() when using multiple threads.
1295+
* \threadsafety All calls should be made from a single thread
12971296
*
12981297
* \since This function is available since SDL 3.0.0.
12991298
*
1300-
* \sa SDL_rand_r
13011299
* \sa SDL_srand
13021300
* \sa SDL_rand_n
13031301
* \sa SDL_rand_float
13041302
*/
13051303
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void);
13061304

1307-
/**
1308-
* Get 32 pseudo-random bits.
1309-
*
1310-
* There are no guarantees as to the quality of the random sequence produced,
1311-
* and this should not be used for security (cryptography, passwords) or where
1312-
* money is on the line (loot-boxes, casinos). There are many random number
1313-
* libraries available with different characteristics and you should pick one of
1314-
* those to meet any serious needs.
1315-
*
1316-
* \param state a pointer to a 64-bit seed value that will be updated with
1317-
* each call to SDL_rand_r(). If the value of the seed is 0, it
1318-
* will be initialized with SDL_GetPerformanceCounter().
1319-
* \returns a random value in the range of [0-SDL_MAX_UINT32], or 0 if state
1320-
* is NULL.
1321-
*
1322-
* \threadsafety This can be called from any thread, however each thread
1323-
* should pass its own state pointer.
1324-
*
1325-
* \since This function is available since SDL 3.0.0.
1326-
*
1327-
* \sa SDL_rand
1328-
*/
1329-
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_r(Uint64 *state);
1330-
13311305
/**
13321306
* Generates a pseudo-random number less than n
13331307
*

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,6 @@
983983
#define SDL_rand SDL_rand_REAL
984984
#define SDL_rand_float SDL_rand_float_REAL
985985
#define SDL_rand_n SDL_rand_n_REAL
986-
#define SDL_rand_r SDL_rand_r_REAL
987986
#define SDL_realloc SDL_realloc_REAL
988987
#define SDL_round SDL_round_REAL
989988
#define SDL_roundf SDL_roundf_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,6 @@ SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallba
992992
SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return)
993993
SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return)
994994
SDL_DYNAPI_PROC(Uint32,SDL_rand_n,(Uint32 a),(a),return)
995-
SDL_DYNAPI_PROC(Uint32,SDL_rand_r,(Uint64 *a),(a),return)
996995
SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
997996
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
998997
SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return)

src/stdlib/SDL_random.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,6 @@ Uint32 SDL_rand(void)
3939
if(!SDL_rand_initialized) {
4040
SDL_srand(0);
4141
}
42-
return SDL_rand_r(&SDL_rand_state);
43-
}
44-
45-
Uint32 SDL_rand_n(Uint32 n)
46-
{
47-
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
48-
Uint64 val = (Uint64)SDL_rand() * n;
49-
return (Uint32)(val >> 32);
50-
}
51-
52-
float SDL_rand_float(void)
53-
{
54-
return (SDL_rand() >> (32-24)) * 0x1p-24f;
55-
}
56-
57-
Uint32 SDL_rand_r(Uint64 *state)
58-
{
59-
if (!state) {
60-
return 0;
61-
}
6242

6343
// The C and A parameters of this LCG have been chosen based on hundreds
6444
// of core-hours of testing with PractRand and TestU01's Crush.
@@ -75,8 +55,20 @@ Uint32 SDL_rand_r(Uint64 *state)
7555
// Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
7656
// https://arxiv.org/abs/2001.05304v2
7757

78-
*state = *state * 0xff1cd035ul + 0x05;
58+
SDL_rand_state = SDL_rand_state * 0xff1cd035ul + 0x05;
7959

8060
// Only return top 32 bits because they have a longer period
81-
return (Uint32)(*state >> 32);
61+
return (Uint32)(SDL_rand_state >> 32);
62+
}
63+
64+
Uint32 SDL_rand_n(Uint32 n)
65+
{
66+
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
67+
Uint64 val = (Uint64)SDL_rand() * n;
68+
return (Uint32)(val >> 32);
69+
}
70+
71+
float SDL_rand_float(void)
72+
{
73+
return (SDL_rand() >> (32-24)) * 0x1p-24f;
8274
}

0 commit comments

Comments
 (0)