The random module provides a pseudo-random number generator and helpers for producing random booleans, integers, numbers, ranges, and selections. Seed the generator when repeatable sequences are needed.
random.seed
function
random
Signature
function random.seed(x)Description
The function
seed initializes the random number generator with a starting value. Using the same seed will produce the same sequence of random numbers.Parameters
x- A number used as the seed value
Example
import random
// Seed with a specific value for reproducible results
random.seed(42)
var a = random.random()
var b = random.random()
// Reset to the same seed
random.seed(42)
var c = random.random()
var d = random.random()
print(a == c) // true
print(b == d) // true
// Seed with current time for different results each run
random.seed(12345)
print("Random value:", random.random())random.random
function
random
Signature
function random.random()Description
The function
random provides a random floating-point number between 0 (inclusive) and 1 (exclusive).Example
import random
// Generate a random decimal
var value = random.random()
print("Random value:", value) // e.g. 0.723456789
// Generate multiple random values
for const i in 0..5
{
print(random.random())
}
// Scale to a custom range
var percentage = random.random() * 100
print("Random percentage:", percentage)random.bool
function
random
Signature
function random.bool()Description
The function
bool provides a random boolean value, either true or false, with equal probability.Example
import random
// Flip a coin
var coin = random.bool()
print("Heads or tails:", coin)
// Simulate a series of coin flips
var heads = 0
var tails = 0
for const i in 0..100
{
if random.bool()
{
heads += 1
}
else
{
tails += 1
}
}
print("Heads: " + heads + ", Tails: " + tails)
// Random decision
if random.bool()
{
print("Taking the left path")
}
else
{
print("Taking the right path")
}random.int
function
random
Signature
function random.int(a, b)Description
The function
int provides a random integer between a and b, inclusive.Parameters
a- The lower bound (inclusive)
b- The upper bound (inclusive)
Returns
A random integer in the range [a, b]
Example
import random
// Roll a six-sided die
var roll = random.int(1, 6)
print("You rolled:", roll)
// Generate a random index
var items = ["apple", "banana", "cherry", "date"]
var idx = random.int(0, len(items) - 1)
print("Selected:", items[idx])
// Random level generation
var level = random.int(1, 100)
print("Generated level:", level)
// Simulate dice rolls
for const i in 0..5
{
print("Roll " + (i + 1) + ": " + random.int(1, 6))
}random.number
function
random
Signature
function random.number(a, b)Description
The function
number provides a random floating-point number between a and b.Parameters
a- The lower bound
b- The upper bound
Returns
A random number in the range [a, b)
Example
import random
// Random temperature between 20.0 and 30.0
var temp = random.number(20.0, 30.0)
print("Temperature:", temp)
// Random position in a game world
var x = random.number(-100.0, 100.0)
var y = random.number(-100.0, 100.0)
print("Spawn position: " + x + " " + y)
// Random probability
var chance = random.number(0.0, 1.0)
if chance < 0.25
{
print("Rare event triggered!")
}
// Random damage calculation
var damage = random.number(10.0, 20.0)
print("Damage dealt:", damage)random.range
function
random
Signature
function random.range(iter)
function random.range(start, end)
function random.range(start, end, step)Description
The function
range provides a random number from a specified range, honoring the range’s step value.Parameters
iter- A range, or the upper bound when used with a single numeric argument
start- The start of the range (inclusive)
end- The end of the range (exclusive)
step- The step between values (default 1)
Example
import random
// Random value from a range
print(random.range(0..10))
// Random value from a range with a step
print(random.range(0..100..10))
// Single numeric argument
print(random.range(50))
// Two numeric arguments
print(random.range(10, 20))
// Three numeric arguments
print(random.range(0, 100, 5))
// Random even number between 0 and 20
print(random.range(0, 20, 2))
// Random multiple of 25 between 0 and 200
print(random.range(0, 200, 25))random.choice
function
random
Signature
function random.choice(list)Description
The function
choice provides a random element from a given list.Parameters
list- The list from which to choose an element
Returns
A randomly selected element from the list
Example
import random
// Pick a random color
var colors = ["red", "green", "blue", "yellow"]
print("Selected color:", random.choice(colors))
// Pick a random card
var deck = ["Ace", "King", "Queen", "Jack", "10", "9", "8"]
print("Drawn card:", random.choice(deck))
// Random reward from a loot table
var loot = ["gold", "silver", "gem", "potion", "scroll"]
print("You found:", random.choice(loot))
// Random enemy encounter
var enemies = ["goblin", "orc", "troll", "dragon"]
var enemy = random.choice(enemies)
print("A wild " + enemy + " appears!")random.shuffle
function
random
Signature
function random.shuffle(list)Description
The function
shuffle randomly reorders the elements of a list in place.Parameters
list- The list to shuffle