mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-02-14 07:43:14 +00:00
Added SDL_srand(), SDL_rand(), and SDL_rand_r() (thanks @JKaniarz!)
These are simple random functions that should not be used for serious random number generation. Fixes https://github.com/libsdl-org/SDL/issues/4968
This commit is contained in:
@@ -955,6 +955,8 @@ SDL3_0.0.0 {
|
||||
SDL_powf;
|
||||
SDL_qsort;
|
||||
SDL_qsort_r;
|
||||
SDL_rand;
|
||||
SDL_rand_r;
|
||||
SDL_realloc;
|
||||
SDL_round;
|
||||
SDL_roundf;
|
||||
@@ -966,6 +968,7 @@ SDL3_0.0.0 {
|
||||
SDL_snprintf;
|
||||
SDL_sqrt;
|
||||
SDL_sqrtf;
|
||||
SDL_srand;
|
||||
SDL_sscanf;
|
||||
SDL_strcasecmp;
|
||||
SDL_strcasestr;
|
||||
|
||||
@@ -980,6 +980,8 @@
|
||||
#define SDL_powf SDL_powf_REAL
|
||||
#define SDL_qsort SDL_qsort_REAL
|
||||
#define SDL_qsort_r SDL_qsort_r_REAL
|
||||
#define SDL_rand SDL_rand_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
|
||||
@@ -991,6 +993,7 @@
|
||||
#define SDL_snprintf SDL_snprintf_REAL
|
||||
#define SDL_sqrt SDL_sqrt_REAL
|
||||
#define SDL_sqrtf SDL_sqrtf_REAL
|
||||
#define SDL_srand SDL_srand_REAL
|
||||
#define SDL_sscanf SDL_sscanf_REAL
|
||||
#define SDL_strcasecmp SDL_strcasecmp_REAL
|
||||
#define SDL_strcasestr SDL_strcasestr_REAL
|
||||
|
||||
@@ -989,6 +989,8 @@ SDL_DYNAPI_PROC(double,SDL_pow,(double a, double b),(a,b),return)
|
||||
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_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_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)
|
||||
@@ -999,6 +1001,7 @@ SDL_DYNAPI_PROC(double,SDL_sin,(double a),(a),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_sinf,(float a),(a),return)
|
||||
SDL_DYNAPI_PROC(double,SDL_sqrt,(double a),(a),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_sqrtf,(float a),(a),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_srand,(Uint64 a),(a),)
|
||||
SDL_DYNAPI_PROC(int,SDL_strcasecmp,(const char *a, const char *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(char*,SDL_strcasestr,(const char *a, const char *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(char*,SDL_strchr,(const char *a, int b),(a,b),return)
|
||||
|
||||
80
src/stdlib/SDL_random.c
Normal file
80
src/stdlib/SDL_random.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
/* This file contains portable random functions for SDL */
|
||||
|
||||
static Uint64 SDL_rand_state;
|
||||
|
||||
void SDL_srand(Uint64 seed)
|
||||
{
|
||||
SDL_rand_state = seed;
|
||||
}
|
||||
|
||||
Uint32 SDL_rand(void)
|
||||
{
|
||||
return SDL_rand_r(&SDL_rand_state);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
Uint64 val = (Uint64)SDL_rand() * n;
|
||||
return (Sint32)(val >> 32);
|
||||
}
|
||||
|
||||
/*
|
||||
* Random float in range [0,1)
|
||||
*/
|
||||
float SDL_rand_float(void)
|
||||
{
|
||||
return (SDL_rand() >> (32-24)) * 0x1p-24f;
|
||||
}
|
||||
|
||||
/* A fast psuedo-random number generator.
|
||||
* Not suitable for cryptography or gambling
|
||||
*/
|
||||
Uint32 SDL_rand_r(Uint64 *state)
|
||||
{
|
||||
if (!state) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!*state) {
|
||||
*state = SDL_GetPerformanceCounter();
|
||||
}
|
||||
|
||||
// Multiplier from Table 6 of
|
||||
// Steele GL, Vigna S. Computationally easy, spectrally good multipliers
|
||||
// for congruential pseudorandom number generators.
|
||||
// Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
|
||||
|
||||
// 32-bit 'a' improves performance on 32-bit architectures
|
||||
// 'c' can be any odd number, but < 256 generates smaller code on some arch
|
||||
*state = *state * 0xf9b25d65ul + 0xFD;
|
||||
|
||||
// Only return top 32 bits because they have a longer period
|
||||
return (Uint32)(*state >> 32);
|
||||
}
|
||||
Reference in New Issue
Block a user