SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Public Member Functions | List of all members

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.
 

Detailed Description

This can be instantiated in any thread and as long as it is not shared with another thread all members are safe to call.

Category:
Wrap state
See also
wrap-state

Member Function Documentation

◆ rand()

Sint32 SDL::Random::rand ( Sint32  n)
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.

Parameters
nthe number of possible outcomes. n must be positive.
Returns
a random value in the range of [0 .. n-1].
Thread safety:
This function is thread-safe, as long as this object isn't shared between threads.
Since
This function is available since SDL 3.2.0.
See also
rand
Random.rand_bits
Random.randf

◆ rand_bits()

Uint32 SDL::Random::rand_bits ( )
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.

Returns
a random value in the range of [0-MAX_UINT32].
Thread safety:
This function is thread-safe, as long as this object isn't shared between threads.
Since
This function is available since SDL 3.2.0.
See also
Random.rand
Random.randf

◆ randf()

float SDL::Random::randf ( )
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.

Returns
a random value in the range of [0.0, 1.0).
Thread safety:
This function is thread-safe, as long as this object isn't shared between threads.
Since
This function is available since SDL 3.2.0.
See also
Random.rand_bits
Random.rand
randf

The documentation for this class was generated from the following file: