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