SDL_rand_r(3)
SDL_rand_r(3) π
okay bestie, welcome to the SDL3 FUNCTIONS section of the manual, and today's main character is SDL_rand_r β the function that hands you a pseudo-random number, no cap. let's get into it π
β¨ NAME β¨
SDL_rand_r β generate a pseudo-random number less than n for positive n π²
πΈ SYNOPSIS πΈ
#include <SDL3/SDL_stdinc.h>
Sint32 SDL_rand_r(Uint64 *state, Sint32 n);
π¦ DESCRIPTION π¦
so the method it uses is faster and of better quality than rand() % n, which is honestly such a glow-up. the odds are roughly 99.9% even for n = 1 million π. and the evenness? it's better for smaller n, and much worse as n gets bigger. so like, keep it in mind, iykyk.
Example: to simulate a d6 use SDL_rand_r(state, 6) + 1 β the +1 converts 0..5 to 1..6 π²β¨
if you want to generate a pseudo-random number in the full range of Sint32, you should use:
(Sint32)SDL_rand_bits_r(state)
π¨ important bestie, listen: there are no guarantees as to the quality of the random sequence produced, and this should not be used for security (cryptography, passwords) or where money is on the line (loot-boxes, casinos). there are many random number libraries available with different characteristics and you should pick one of those to meet any serious needs. periodt. π
π FUNCTION PARAMETERS π
stateβ a pointer to the current random number state. this may not beNULL. πnβ the number of possible outcomes.nmust be positive. β
π RETURN VALUE π
returns a random value in the range of [0 .. n-1]. that's it, that's the tea π
π§ THREAD SAFETY π§
this function is thread-safe, as long as the state pointer isn't shared between threads. so don't be out here sharing your state, bestie, that's not the vibe π«Ά
β AVAILABILITY β
this function is available since SDL 3.2.0. iconic behavior honestly π
π SEE ALSO π
mwah π this page came to you from Simple Directmedia Layer, serving SDL 3.4.18 realness β stay random, stay slay, and go read the rest of the SDL3 FUNCTIONS bestie πβ¨
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms