Random Module
The random module provides pseudorandom number generation for Mux programs. It uses a time-based seed by default, but can be manually seeded for reproducible results.
Import
Functions
random.seed
Initialize the random number generator with a specific seed. Use this when you need reproducible random sequences, such as in testing or simulations.
random.next_int
Generate a random integer between 0 and RAND_MAX (platform-dependent, typically 2,147,483,647).
The generator auto-initializes with the current time on first use if not explicitly seeded.
random.next_range
Generate a random integer in the range [min, max). The lower bound is inclusive, the upper bound is exclusive.
Returns min if min >= max.
random.next_float
Generate a random floating-point number in the range [0.0, 1.0).
Useful for probabilities, animations, and scientific calculations.
random.next_bool
Generate a random boolean value (true or false) with equal probability (50/50).
Complete Example
Implementation Details
The random module uses the standard C library's rand() function with the following characteristics:
- Thread safety: Uses atomic operations for initialization state
- Auto-initialization: Seeds automatically with current time if not explicitly seeded
- Range distribution: Uses modulo arithmetic for
next_range, which may have slight bias for very large ranges - Float precision: Produces values with approximately 15 bits of precision
For cryptographic applications or high-precision simulations, consider using a specialized library.