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
random.seed(int seed) returns voidInitialize 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
random.next_int() returns intGenerate 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
random.next_range(int min, int max) returns intGenerate 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
random.next_float() returns floatGenerate a random floating-point number in the range [0.0, 1.0).
Useful for probabilities, animations, and scientific calculations.
random.next_bool
random.next_bool() returns boolGenerate a random boolean value (true or false) with equal probability (50/50).
Complete Example
Implementation Details
The random module uses a custom Linear Congruential Generator (LCG) with the following characteristics:
- Thread safety: Uses a
Mutex-protected state withOnce-based initialization - Auto-initialization: Seeds automatically with current time if not explicitly seeded
- Range distribution:
next_rangescales the generator's output by the span with a fixed-point multiply rather than taking a remainder. Like any method without a rejection step this leaves a slight bias, growing with the size of the range relative to the generator's 31-bit output - Float precision: Produces values with approximately 31 bits of precision
For cryptographic applications or high-precision simulations, consider using a specialized library.