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

@@ -23,7 +23,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdio.h>
#include <stdlib.h>
static int done;
@@ -172,8 +171,7 @@ static void loop(void)
/* Check for events */
/*SDL_WaitEvent(&event); emscripten does not like waiting*/
(void)fprintf(stderr, "starting loop\n");
(void)fflush(stderr);
SDL_Log("starting loop\n");
while (!done && SDL_WaitEvent(&event)) {
SDL_Log("Got event type: %" SDL_PRIu32 "\n", event.type);
switch (event.type) {
@@ -189,8 +187,7 @@ static void loop(void)
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
/* Left button quits the app, other buttons toggles text input */
(void)fprintf(stderr, "mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT);
(void)fflush(stderr);
SDL_Log("mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT);
if (event.button.button == SDL_BUTTON_LEFT) {
done = 1;
} else {
@@ -209,11 +206,9 @@ static void loop(void)
default:
break;
}
(void)fprintf(stderr, "waiting new event\n");
(void)fflush(stderr);
SDL_Log("waiting new event\n");
}
(void)fprintf(stderr, "exiting event loop\n");
(void)fflush(stderr);
SDL_Log("exiting event loop\n");
#ifdef SDL_PLATFORM_EMSCRIPTEN
if (done) {
emscripten_cancel_main_loop();
@@ -228,12 +223,11 @@ static int SDLCALL ping_thread(void *ptr)
SDL_Event sdlevent;
SDL_memset(&sdlevent, 0, sizeof(SDL_Event));
for (cnt = 0; cnt < 10; ++cnt) {
(void)fprintf(stderr, "sending event (%d/%d) from thread.\n", cnt + 1, 10);
(void)fflush(stderr);
SDL_Log("sending event (%d/%d) from thread.\n", cnt + 1, 10);
sdlevent.type = SDL_EVENT_KEY_DOWN;
sdlevent.key.keysym.sym = SDLK_1;
SDL_PushEvent(&sdlevent);
SDL_Delay(1000 + rand() % 1000);
SDL_Delay(1000 + SDL_rand() % 1000);
}
return cnt;
}

View File

@@ -20,9 +20,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
#define NUM_OBJECTS 100
static SDLTest_CommonState *state;
@@ -75,8 +72,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);
}
}
@@ -123,10 +120,10 @@ static void DrawLines(SDL_Renderer *renderer)
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
} else {
x1 = (float)((rand() % (viewport.w * 2)) - viewport.w);
x2 = (float)((rand() % (viewport.w * 2)) - viewport.w);
y1 = (float)((rand() % (viewport.h * 2)) - viewport.h);
y2 = (float)((rand() % (viewport.h * 2)) - viewport.h);
x1 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w);
x2 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w);
y1 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h);
y2 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h);
SDL_RenderLine(renderer, x1, y1, x2, y2);
}
}
@@ -168,10 +165,10 @@ static void DrawRects(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
rect.w = (float)(rand() % (viewport.h / 2));
rect.h = (float)(rand() % (viewport.h / 2));
rect.x = (float)((rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2));
rect.w = (float)(SDL_rand() % (viewport.h / 2));
rect.h = (float)(SDL_rand() % (viewport.h / 2));
rect.x = (float)((SDL_rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((SDL_rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2));
SDL_RenderFillRect(renderer, &rect);
}
}
@@ -293,8 +290,6 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
}
srand((unsigned int)time(NULL));
/* Main render loop */
frames = 0;
next_fps_check = SDL_GetTicks() + fps_check_delay;

View File

@@ -14,9 +14,6 @@
* For a more complete video example, see ffplay.c in the ffmpeg sources.
*/
#include <stdlib.h>
#include <time.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
@@ -1486,17 +1483,16 @@ int main(int argc, char *argv[])
/* Position sprites and set their velocities */
SDL_Rect viewport;
SDL_GetRenderViewport(renderer, &viewport);
srand((unsigned int)time(NULL));
for (i = 0; i < num_sprites; ++i) {
positions[i].x = (float)(rand() % (viewport.w - sprite_w));
positions[i].y = (float)(rand() % (viewport.h - sprite_h));
positions[i].x = (float)(SDL_rand() % (viewport.w - sprite_w));
positions[i].y = (float)(SDL_rand() % (viewport.h - sprite_h));
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f || velocities[i].y == 0.f) {
velocities[i].x = (float)((rand() % (2 + 1)) - 1);
velocities[i].y = (float)((rand() % (2 + 1)) - 1);
velocities[i].x = (float)((SDL_rand() % (2 + 1)) - 1);
velocities[i].y = (float)((SDL_rand() % (2 + 1)) - 1);
}
}

View File

@@ -12,6 +12,8 @@
/* Simple program: draw a RGB triangle, with texture */
#include <stdlib.h>
#include "testutils.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
@@ -21,9 +23,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
static SDLTest_CommonState *state;
static SDL_bool use_texture = SDL_FALSE;
static SDL_Texture **sprites;
@@ -260,8 +259,6 @@ int main(int argc, char *argv[])
}
}
srand((unsigned int)time(NULL));
/* Main render loop */
frames = 0;
then = SDL_GetTicks();

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();

View File

@@ -11,6 +11,8 @@
*/
/* Simple program: Create a native window and attach an SDL renderer */
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
@@ -18,9 +20,6 @@
#include "testnative.h"
#include "testutils.h"
#include <stdlib.h> /* for srand() */
#include <time.h> /* for time() */
#define WINDOW_W 640
#define WINDOW_H 480
#define NUM_SPRITES 100
@@ -188,17 +187,16 @@ int main(int argc, char *argv[])
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
quit(2);
}
srand((unsigned int)time(NULL));
for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(rand() % (window_w - (int)sprite_w));
positions[i].y = (float)(rand() % (window_h - (int)sprite_h));
positions[i].x = (float)(SDL_rand() % (window_w - (int)sprite_w));
positions[i].y = (float)(SDL_rand() % (window_h - (int)sprite_h));
positions[i].w = sprite_w;
positions[i].h = sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
}
}

View File

@@ -21,9 +21,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
static SDL_Renderer *renderer = NULL;
static SDL_Window *window = NULL;
static int done = SDL_FALSE;
@@ -137,8 +134,6 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
srand((unsigned int)time(NULL));
#ifndef SDL_PLATFORM_EMSCRIPTEN
/* Main render loop */
frames = 0;

View File

@@ -20,7 +20,6 @@ freely.
#endif
#include <stdlib.h>
#include <time.h>
#define MENU_WIDTH 120
#define MENU_HEIGHT 300

View File

@@ -19,9 +19,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
static SDLTest_CommonState *state;
static int i, done;
static float mouseX, mouseY;
@@ -115,7 +112,6 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
}
srand((unsigned int)time(NULL));
if (SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
return 3;
}

View File

@@ -18,9 +18,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
#include "icon.h"
#define WINDOW_WIDTH 640
@@ -136,17 +133,16 @@ int main(int argc, char *argv[])
}
/* Initialize the sprite positions */
srand((unsigned int)time(NULL));
for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(rand() % (WINDOW_WIDTH - sprite_w));
positions[i].y = (float)(rand() % (WINDOW_HEIGHT - sprite_h));
positions[i].x = (float)(SDL_rand() % (WINDOW_WIDTH - sprite_w));
positions[i].y = (float)(SDL_rand() % (WINDOW_HEIGHT - sprite_h));
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
}
}

View File

@@ -10,9 +10,6 @@
freely.
*/
#include <stdlib.h>
#include <time.h>
#include <SDL3/SDL.h>
#include <wayland-client.h>
#include <xdg-shell-client-protocol.h>
@@ -99,17 +96,16 @@ static int InitSprites(void)
return -1;
}
srand((unsigned int)time(NULL));
for (int i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(rand() % (WINDOW_WIDTH - sprite_w));
positions[i].y = (float)(rand() % (WINDOW_HEIGHT - sprite_h));
positions[i].x = (float)(SDL_rand() % (WINDOW_WIDTH - sprite_w));
positions[i].y = (float)(SDL_rand() % (WINDOW_HEIGHT - sprite_h));
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED);
}
}