SDL_rand(3)
π NAME π
SDL_rand β bestie, it generates a pseudo-random number less than n, for positive n π (yes this is from the SDL3 FUNCTIONS manual, we're fancy here)
πΈ SYNOPSIS πΈ
#include <SDL3/SDL_stdinc.h>
Sint32 SDL_rand(Sint32 n);
β¨ DESCRIPTION β¨
okay so the method this uses is faster and of better quality than rand() % n. like it's not even a competition. the odds come out roughly 99.9% even even when n = 1 million. and the evenness is even better for smaller nβ¦ but it gets much worse as n gets bigger. so know that going in π¦
Example: to simulate a d6 use SDL_rand(6) + 1. The +1 converts 0..5 to 1..6. π² ate, no crumbs.
If you want to generate a pseudo-random number in the full range of Sint32, you should use:
(Sint32)SDL_rand_bits()
If you want reproducible output, be sure to initialize with SDL_srand() first. fr fr, do it first or the vibes will not be repeatable.
π¨ no cap, and this is the important part: 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). π please don't put your crypto keys on this i am begging.
There are many random number libraries available with different characteristics, and you should pick one of those to meet any serious needs. this one is for fun little numbers, not for the serious stuff, periodt.
π FUNCTION PARAMETERS π
- n β the number of possible outcomes. n must be positive. that's the whole rule, and she means it π
π₯ RETURN VALUE π₯
Returns a random value in the range of [0 .. n-1]. inclusive bottom, exclusive top, the way that it is, iconic range honestly β¨
π§ THREAD SAFETY π§
All calls should be made from a single thread. one thread. that's it. don't be messy about it π
π AVAILABILITY π
This function is available since SDL 3.2.0. so if you're on something olderβ¦ bestie, upgrade π
π SEE ALSO π
and that's the whole page!! brought to you by Simple Directmedia Layer, version SDL 3.4.18, and it's the SDL_rand(3) page π go generate some numbers, love u, bye π«Ά
π¦ 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