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 be NULL. πŸ™ˆ
  • n β€” the number of possible outcomes. n must 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