Skip to content

Commit 8f29f8c

Browse files
JKaniarzslouken
authored andcommitted
Renamed SDL_rand() to SDL_rand_bits() and updated tests
1 parent 237bbfc commit 8f29f8c

File tree

12 files changed

+110
-98
lines changed

12 files changed

+110
-98
lines changed

include/SDL3/SDL_stdinc.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,25 +1260,27 @@ extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRI
12601260
extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
12611261

12621262
/**
1263-
* Seed the pseudo-random number generator.
1263+
* Seeds the pseudo-random number generator.
12641264
*
1265-
* Reusing the seed number will cause SDL_rand() to repeat the same stream of
1266-
* 'random' numbers.
1265+
* Reusing the seed number will cause SDL_rand_*() to repeat the same stream
1266+
* of 'random' numbers.
12671267
*
12681268
* \param seed the value to use as a random number seed, or 0 to use
12691269
* SDL_GetPerformanceCounter().
12701270
*
12711271
* \threadsafety This should be called on the same thread that calls
1272-
* SDL_rand()
1272+
* SDL_rand*()
12731273
*
12741274
* \since This function is available since SDL 3.0.0.
12751275
*
1276-
* \sa SDL_rand
1276+
* \sa SDL_rand_n
1277+
* \sa SDL_rand_float
1278+
* \sa SDL_rand_bits
12771279
*/
12781280
extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
12791281

12801282
/**
1281-
* Get 32 pseudo-random bits.
1283+
* Generates 32 pseudo-random bits.
12821284
*
12831285
* You likely want to use SDL_rand_n() to get a psuedo-randum number instead.
12841286
*
@@ -1301,38 +1303,44 @@ extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
13011303
* \sa SDL_rand_n
13021304
* \sa SDL_rand_float
13031305
*/
1304-
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void);
1306+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
13051307

13061308
/**
1307-
* Generates a pseudo-random number less than n
1309+
* Generates a pseudo-random number less than n for positive n
13081310
*
1309-
* The method used is faster and of better quality than `SDL_rand() % n`.
1310-
* However just like with `SDL_rand() % n`, bias increases with larger n. Odds
1311-
* are better than 99.9% even for n under 1 million.
1311+
* The method used is faster and of better quality than `rand() % n`.
1312+
* Odds are roughly 99.9% even for n = 1 million. Evenness is better for
1313+
* smaller n, and much worse as n gets bigger.
13121314
*
13131315
* Example: to simulate a d6 use `SDL_rand_n(6) + 1` The +1 converts 0..5 to
13141316
* 1..6
13151317
*
1318+
* If you want reproducible output, be sure to initialize with SDL_srand() first.
1319+
*
13161320
* There are no guarantees as to the quality of the random sequence produced,
13171321
* and this should not be used for security (cryptography, passwords) or where
13181322
* money is on the line (loot-boxes, casinos). There are many random number
13191323
* libraries available with different characteristics and you should pick one
13201324
* of those to meet any serious needs.
13211325
*
1322-
* \param n the number of possible values.
1323-
* \returns a random value in the range of [0 .. n-1].
1326+
* \param n the number of possible outcomes. n must be positive.
1327+
*
1328+
* \returns a random value in the range of [0 .. n-1]
13241329
*
13251330
* \threadsafety All calls should be made from a single thread
13261331
*
13271332
* \since This function is available since SDL 3.0.0.
13281333
*
1329-
* \sa SDL_rand
1334+
* \sa SDL_srand
1335+
* \sa SDL_rand_float
13301336
*/
1331-
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n);
1337+
extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_n(Sint32 n);
13321338

13331339
/**
13341340
* Generates a uniform pseudo-random floating point number less than 1.0
13351341
*
1342+
* If you want reproducible output, be sure to initialize with SDL_srand() first.
1343+
*
13361344
* There are no guarantees as to the quality of the random sequence produced,
13371345
* and this should not be used for security (cryptography, passwords) or where
13381346
* money is on the line (loot-boxes, casinos). There are many random number
@@ -1345,7 +1353,8 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n);
13451353
*
13461354
* \since This function is available since SDL 3.0.0.
13471355
*
1348-
* \sa SDL_rand
1356+
* \sa SDL_srand
1357+
* \sa SDL_rand_n
13491358
*/
13501359
extern SDL_DECLSPEC float SDLCALL SDL_rand_float(void);
13511360

src/dynapi/SDL_dynapi.sym

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,9 @@ SDL3_0.0.0 {
955955
SDL_powf;
956956
SDL_qsort;
957957
SDL_qsort_r;
958-
SDL_rand;
959-
SDL_rand_r;
958+
SDL_rand_bits;
959+
SDL_rand_float;
960+
SDL_rand_n;
960961
SDL_realloc;
961962
SDL_round;
962963
SDL_roundf;

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@
980980
#define SDL_powf SDL_powf_REAL
981981
#define SDL_qsort SDL_qsort_REAL
982982
#define SDL_qsort_r SDL_qsort_r_REAL
983-
#define SDL_rand SDL_rand_REAL
983+
#define SDL_rand_bits SDL_rand_bits_REAL
984984
#define SDL_rand_float SDL_rand_float_REAL
985985
#define SDL_rand_n SDL_rand_n_REAL
986986
#define SDL_realloc SDL_realloc_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,9 @@ SDL_DYNAPI_PROC(double,SDL_pow,(double a, double b),(a,b),return)
989989
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),)
992-
SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return)
992+
SDL_DYNAPI_PROC(Uint32,SDL_rand_bits,(void),(),return)
993993
SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return)
994-
SDL_DYNAPI_PROC(Uint32,SDL_rand_n,(Uint32 a),(a),return)
994+
SDL_DYNAPI_PROC(Sint32,SDL_rand_n,(Sint32 a),(a),return)
995995
SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
996996
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
997997
SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return)

src/stdlib/SDL_random.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void SDL_srand(Uint64 seed)
3434
SDL_rand_initialized = SDL_TRUE;
3535
}
3636

37-
Uint32 SDL_rand(void)
37+
Uint32 SDL_rand_bits(void)
3838
{
3939
if(!SDL_rand_initialized) {
4040
SDL_srand(0);
@@ -61,14 +61,14 @@ Uint32 SDL_rand(void)
6161
return (Uint32)(SDL_rand_state >> 32);
6262
}
6363

64-
Uint32 SDL_rand_n(Uint32 n)
64+
Sint32 SDL_rand_n(Sint32 n)
6565
{
6666
// 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);
67+
Uint64 val = (Uint64)SDL_rand_bits() * n;
68+
return (Sint32)(val >> 32);
6969
}
7070

7171
float SDL_rand_float(void)
7272
{
73-
return (SDL_rand() >> (32-24)) * 0x1p-24f;
73+
return (SDL_rand_bits() >> (32-24)) * 0x1p-24f;
7474
}

test/checkkeysthreads.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void loop(void)
206206
default:
207207
break;
208208
}
209-
SDL_Log("waiting new event\n");
209+
SDL_Log("waiting new event\n");
210210
}
211211
SDL_Log("exiting event loop\n");
212212
#ifdef SDL_PLATFORM_EMSCRIPTEN
@@ -227,7 +227,7 @@ static int SDLCALL ping_thread(void *ptr)
227227
sdlevent.type = SDL_EVENT_KEY_DOWN;
228228
sdlevent.key.keysym.sym = SDLK_1;
229229
SDL_PushEvent(&sdlevent);
230-
SDL_Delay(1000 + SDL_rand() % 1000);
230+
SDL_Delay(1000 + SDL_rand_n(1000));
231231
}
232232
return cnt;
233233
}

test/testdraw.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ static void DrawPoints(SDL_Renderer *renderer)
7272
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
7373
(Uint8)current_color, (Uint8)current_alpha);
7474

75-
x = (float)(SDL_rand() % viewport.w);
76-
y = (float)(SDL_rand() % viewport.h);
75+
x = (float)SDL_rand_n(viewport.w);
76+
y = (float)SDL_rand_n(viewport.h);
7777
SDL_RenderPoint(renderer, x, y);
7878
}
7979
}
@@ -120,10 +120,10 @@ static void DrawLines(SDL_Renderer *renderer)
120120
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
121121
SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
122122
} else {
123-
x1 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w);
124-
x2 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w);
125-
y1 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h);
126-
y2 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h);
123+
x1 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
124+
x2 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
125+
y1 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
126+
y2 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
127127
SDL_RenderLine(renderer, x1, y1, x2, y2);
128128
}
129129
}
@@ -165,10 +165,10 @@ static void DrawRects(SDL_Renderer *renderer)
165165
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
166166
(Uint8)current_color, (Uint8)current_alpha);
167167

168-
rect.w = (float)(SDL_rand() % (viewport.h / 2));
169-
rect.h = (float)(SDL_rand() % (viewport.h / 2));
170-
rect.x = (float)((SDL_rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2));
171-
rect.y = (float)((SDL_rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2));
168+
rect.w = (float)SDL_rand_n(viewport.h / 2);
169+
rect.h = (float)SDL_rand_n(viewport.h / 2);
170+
rect.x = (float)((SDL_rand_n(viewport.w * 2) - viewport.w) - (rect.w / 2));
171+
rect.y = (float)((SDL_rand_n(viewport.h * 2) - viewport.h) - (rect.h / 2));
172172
SDL_RenderFillRect(renderer, &rect);
173173
}
174174
}

0 commit comments

Comments
 (0)