std.random

The std.random module provides a comprehensive set of functions for random number generation, including basic random numbers, range-based selections, and various probability distributions.

seed(value: int) -> void

Sets the seed for the random number generator to produce reproducible sequences of random numbers.

Parameters:

  • value: An integer value to use as the seed

Example:

use std.random;

random.seed(42);
// Now all subsequent random operations will produce the same sequence

random() -> float

Generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive).

Returns:

  • A random float in the range [0.0, 1.0)

Example:

use std.random;

let value = random.random();
print(value); // Outputs: 0.7123889...

randint(min: int, max: int) -> int

Generates a random integer between min and max (inclusive).

Parameters:

  • min: The lower bound (inclusive)
  • max: The upper bound (inclusive)

Returns:

  • A random integer in the range [min, max]

Example:

use std.random;

let dice_roll = random.randint(1, 6);
print(dice_roll); // Outputs a number between 1 and 6

uniform(min: float, max: float) -> float

Generates a random floating-point number from a uniform distribution between min and max.

Parameters:

  • min: The lower bound (inclusive)
  • max: The upper bound (exclusive)

Returns:

  • A random float in the range [min, max)

Example:

use std.random;

let temperature = random.uniform(20.0, 30.0);
print(temperature); // Outputs: 25.7392...

range(start: int, end: int, step?: int) -> int

Generates a random integer from a range with optional step size.

Parameters:

  • start: The start of the range (inclusive)
  • end: The end of the range (exclusive)
  • step: (Optional) The step size between numbers

Returns:

  • A random integer from the specified range

Example:

use std.random;

// Random even number between 0 and 10
let even = random.range(0, 10, 2);
print(even); // Outputs: 0, 2, 4, 6, or 8

choice(list: array) -> any

Randomly selects an element from a list.

Parameters:

  • list: An array of elements to choose from

Returns:

  • A random element from the list

Example:

use std.random;

let colors = ["red", "green", "blue"];
let color = random.choice(colors);
print(color); // Outputs: one of the colors

normal(mean: float, std_dev: float) -> float

Generates a random floating-point number from a normal (Gaussian) distribution.

Parameters:

  • mean: The mean (average) of the distribution
  • std_dev: The standard deviation of the distribution

Returns:

  • A random float from the normal distribution

Example:

use std.random;

// Generate IQ scores (mean=100, std_dev=15)
let iq = random.normal(100.0, 15.0);
print(iq); // Outputs: 97.234...

exponential(lambda: float) -> float

Generates a random floating-point number from an exponential distribution.

Parameters:

  • lambda: The rate parameter (λ) of the exponential distribution

Returns:

  • A random float from the exponential distribution

Example:

use std.random;

// Generate waiting times between events
let wait_time = random.exponential(0.5);
print(wait_time); // Outputs: 1.386...

bool() -> boolean

Generates a random boolean value.

Returns:

  • A random boolean (true or false)

Example:

use std.random;

let coin_flip = random.bool();
print(coin_flip); // Outputs: true or false