SDL3pp
A slim C++ wrapper for SDL3
|
A independent pseudo random state. More...
Public Member Functions | |
constexpr | Random (Uint64 state) |
Init state with the given value. | |
constexpr | operator Uint64 () |
Convert to the underlying type. | |
Sint32 | rand (Sint32 n) |
Generate a pseudo-random number less than n for positive n. | |
float | randf () |
Generate a uniform pseudo-random floating point number less than 1.0. | |
Uint32 | rand_bits () |
Generate 32 pseudo-random bits. | |
This can be instantiated in any thread and as long as it is not shared with another thread all members are safe to call.
|
inline |
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 state.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)state.rand_bits()
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.
n | the number of possible outcomes. n must be positive. |
|
inline |
You likely want to use Random.rand() to get a pseudo-random number instead.
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.
|
inline |
If you want reproducible output, be sure to initialize with 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.