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:
Sam Lantinga 2024-06-16 07:20:11 -07:00
parent 9cb4bb92f6
commit d1d484ddbe
23 changed files with 203 additions and 89 deletions

View file

@ -19,9 +19,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
#define SWAP(typ, a, b) \
do { \
typ t = a; \
@ -77,8 +74,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
x = (float)(rand() % viewport.w);
y = (float)(rand() % viewport.h);
x = (float)(SDL_rand() % viewport.w);
y = (float)(SDL_rand() % viewport.h);
SDL_RenderPoint(renderer, x, y);
}
}
@ -234,10 +231,10 @@ static void loop(void *arg)
num_lines = 0;
} else {
add_line(
(float)(rand() % 640),
(float)(rand() % 480),
(float)(rand() % 640),
(float)(rand() % 480));
(float)(SDL_rand() % 640),
(float)(SDL_rand() % 480),
(float)(SDL_rand() % 640),
(float)(SDL_rand() % 480));
}
break;
case 'r':
@ -245,10 +242,10 @@ static void loop(void *arg)
num_rects = 0;
} else {
add_rect(
(float)(rand() % 640),
(float)(rand() % 480),
(float)(rand() % 640),
(float)(rand() % 480));
(float)(SDL_rand() % 640),
(float)(SDL_rand() % 480),
(float)(SDL_rand() % 640),
(float)(SDL_rand() % 480));
}
break;
default:
@ -363,8 +360,6 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
}
srand((unsigned int)time(NULL));
/* Main render loop */
frames = 0;
then = SDL_GetTicks();