mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-25 14:09:10 +00:00
Added SDL_rand_float and SDL_rand_n to API
This commit is contained in:
parent
f4ee59a1a2
commit
83d21e20df
4 changed files with 47 additions and 9 deletions
|
@ -1319,6 +1319,47 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void);
|
||||||
*/
|
*/
|
||||||
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_r(Uint64 *state);
|
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_r(Uint64 *state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a pseudo-random number less than n
|
||||||
|
*
|
||||||
|
* The method used is faster and of better quality than `SDL_rand() % n`.
|
||||||
|
* However just like with `SDL_rand() % n`, bias increases with larger n.
|
||||||
|
*
|
||||||
|
* Example: to simulate a d6 use `SDL_rand_n(6) + 1`
|
||||||
|
* The +1 converts 0..5 to 1..6
|
||||||
|
*
|
||||||
|
* There are no guarantees as to the quality of the random sequence produced,
|
||||||
|
* and this should not be used for cryptography or anything that requires good
|
||||||
|
* random distribution.
|
||||||
|
*
|
||||||
|
* \param n the number of possible values
|
||||||
|
*
|
||||||
|
* \returns a random value in the range of [0 .. n-1]
|
||||||
|
*
|
||||||
|
* \threadsafety All calls should be made from a single thread
|
||||||
|
*
|
||||||
|
* \since This function is available since SDL 3.0.0.
|
||||||
|
*
|
||||||
|
* \sa SDL_rand
|
||||||
|
*/
|
||||||
|
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a pseudo-random floating point number less than 1.0
|
||||||
|
*
|
||||||
|
* There are no guarantees as to the quality of the random sequence produced,
|
||||||
|
* and this should not be used for cryptography or anything that requires good
|
||||||
|
* random distribution.
|
||||||
|
*
|
||||||
|
* \returns a random value in the range of [0.0, 1.0)
|
||||||
|
*
|
||||||
|
* \threadsafety All calls should be made from a single thread
|
||||||
|
*
|
||||||
|
* \since This function is available since SDL 3.0.0.
|
||||||
|
*
|
||||||
|
* \sa SDL_rand
|
||||||
|
*/
|
||||||
|
extern SDL_DECLSPEC float SDLCALL SDL_rand_float(void);
|
||||||
|
|
||||||
#ifndef SDL_PI_D
|
#ifndef SDL_PI_D
|
||||||
#define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
|
#define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
|
||||||
|
|
|
@ -981,6 +981,8 @@
|
||||||
#define SDL_qsort SDL_qsort_REAL
|
#define SDL_qsort SDL_qsort_REAL
|
||||||
#define SDL_qsort_r SDL_qsort_r_REAL
|
#define SDL_qsort_r SDL_qsort_r_REAL
|
||||||
#define SDL_rand SDL_rand_REAL
|
#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_rand_r SDL_rand_r_REAL
|
||||||
#define SDL_realloc SDL_realloc_REAL
|
#define SDL_realloc SDL_realloc_REAL
|
||||||
#define SDL_round SDL_round_REAL
|
#define SDL_round SDL_round_REAL
|
||||||
|
|
|
@ -990,6 +990,8 @@ SDL_DYNAPI_PROC(float,SDL_powf,(float a, float b),(a,b),return)
|
||||||
SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),)
|
SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),)
|
||||||
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),)
|
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),)
|
||||||
SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return)
|
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(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(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
|
||||||
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
|
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
|
||||||
|
|
|
@ -42,20 +42,13 @@ Uint32 SDL_rand(void)
|
||||||
return SDL_rand_r(&SDL_rand_state);
|
return SDL_rand_r(&SDL_rand_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
Uint32 SDL_rand_n(Uint32 n)
|
||||||
* Return a number between [0, n)
|
|
||||||
* Fast but slightly biased. Don't run your casino with this.
|
|
||||||
*/
|
|
||||||
Sint32 SDL_rand_n(Sint32 n)
|
|
||||||
{
|
{
|
||||||
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
|
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
|
||||||
Uint64 val = (Uint64)SDL_rand() * n;
|
Uint64 val = (Uint64)SDL_rand() * n;
|
||||||
return (Sint32)(val >> 32);
|
return (Uint32)(val >> 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Random float in range [0,1)
|
|
||||||
*/
|
|
||||||
float SDL_rand_float(void)
|
float SDL_rand_float(void)
|
||||||
{
|
{
|
||||||
return (SDL_rand() >> (32-24)) * 0x1p-24f;
|
return (SDL_rand() >> (32-24)) * 0x1p-24f;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue