Skip to content

Commit 83d21e2

Browse files
JKaniarzslouken
authored andcommitted
Added SDL_rand_float and SDL_rand_n to API
1 parent f4ee59a commit 83d21e2

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

include/SDL3/SDL_stdinc.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,47 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void);
13191319
*/
13201320
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_r(Uint64 *state);
13211321

1322+
/**
1323+
* Generates a pseudo-random number less than n
1324+
*
1325+
* The method used is faster and of better quality than `SDL_rand() % n`.
1326+
* However just like with `SDL_rand() % n`, bias increases with larger n.
1327+
*
1328+
* Example: to simulate a d6 use `SDL_rand_n(6) + 1`
1329+
* The +1 converts 0..5 to 1..6
1330+
*
1331+
* There are no guarantees as to the quality of the random sequence produced,
1332+
* and this should not be used for cryptography or anything that requires good
1333+
* random distribution.
1334+
*
1335+
* \param n the number of possible values
1336+
*
1337+
* \returns a random value in the range of [0 .. n-1]
1338+
*
1339+
* \threadsafety All calls should be made from a single thread
1340+
*
1341+
* \since This function is available since SDL 3.0.0.
1342+
*
1343+
* \sa SDL_rand
1344+
*/
1345+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n);
1346+
1347+
/**
1348+
* Generates a pseudo-random floating point number less than 1.0
1349+
*
1350+
* There are no guarantees as to the quality of the random sequence produced,
1351+
* and this should not be used for cryptography or anything that requires good
1352+
* random distribution.
1353+
*
1354+
* \returns a random value in the range of [0.0, 1.0)
1355+
*
1356+
* \threadsafety All calls should be made from a single thread
1357+
*
1358+
* \since This function is available since SDL 3.0.0.
1359+
*
1360+
* \sa SDL_rand
1361+
*/
1362+
extern SDL_DECLSPEC float SDLCALL SDL_rand_float(void);
13221363

13231364
#ifndef SDL_PI_D
13241365
#define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,8 @@
981981
#define SDL_qsort SDL_qsort_REAL
982982
#define SDL_qsort_r SDL_qsort_r_REAL
983983
#define SDL_rand SDL_rand_REAL
984+
#define SDL_rand_float SDL_rand_float_REAL
985+
#define SDL_rand_n SDL_rand_n_REAL
984986
#define SDL_rand_r SDL_rand_r_REAL
985987
#define SDL_realloc SDL_realloc_REAL
986988
#define SDL_round SDL_round_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ SDL_DYNAPI_PROC(float,SDL_powf,(float a, float b),(a,b),return)
990990
SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),)
991991
SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallback_r d, void *e),(a,b,c,d,e),)
992992
SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return)
993+
SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return)
994+
SDL_DYNAPI_PROC(Uint32,SDL_rand_n,(Uint32 a),(a),return)
993995
SDL_DYNAPI_PROC(Uint32,SDL_rand_r,(Uint64 *a),(a),return)
994996
SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
995997
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)

src/stdlib/SDL_random.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,13 @@ Uint32 SDL_rand(void)
4242
return SDL_rand_r(&SDL_rand_state);
4343
}
4444

45-
/*
46-
* Return a number between [0, n)
47-
* Fast but slightly biased. Don't run your casino with this.
48-
*/
49-
Sint32 SDL_rand_n(Sint32 n)
45+
Uint32 SDL_rand_n(Uint32 n)
5046
{
5147
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
5248
Uint64 val = (Uint64)SDL_rand() * n;
53-
return (Sint32)(val >> 32);
49+
return (Uint32)(val >> 32);
5450
}
5551

56-
/*
57-
* Random float in range [0,1)
58-
*/
5952
float SDL_rand_float(void)
6053
{
6154
return (SDL_rand() >> (32-24)) * 0x1p-24f;

0 commit comments

Comments
 (0)