Skip to main content

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

snippet.mux
Loading...

Functions

random.seed

snippet.mux
Loading...

Initialize the random number generator with a specific seed. Use this when you need reproducible random sequences, such as in testing or simulations.

seed_example.mux
Loading...

random.next_int

snippet.mux
Loading...

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.

dice_roll.mux
Loading...

random.next_range

snippet.mux
Loading...

Generate a random integer in the range [min, max). The lower bound is inclusive, the upper bound is exclusive.

Returns min if min >= max.

lottery_numbers.mux
Loading...

random.next_float

snippet.mux
Loading...

Generate a random floating-point number in the range [0.0, 1.0).

Useful for probabilities, animations, and scientific calculations.

probability_example.mux
Loading...

random.next_bool

snippet.mux
Loading...

Generate a random boolean value (true or false) with equal probability (50/50).

coin_flip.mux
Loading...

Complete Example

random_demo.mux
Loading...

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.