Generate a pseudo-random number less than n for positive n
Defined in <SDL3/SDL_stdinc.h>
Sint32 SDL_rand(Sint32 n);
Sint32 | n | the number of possible outcomes. n must be positive. |
(Sint32) Returns a random value in the range of [0 .. n-1].
The method used is faster and of better quality than rand() % n
. Odds are roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and much worse as n gets bigger.
Example: to simulate a d6 use SDL_rand(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()
If you want reproducible output, be sure to initialize with SDL_srand() first.
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.
All calls should be made from a single thread
This function is available since SDL 3.1.3.