Skip to content

random

This content is for v1.0. Switch to the latest version for up-to-date documentation.

The random package provides pseudo-random number generation functions. It is available globally without an import statement.

Returns a pseudo-random integer in the range [min, max] (inclusive of both boundaries).

  • Parameters:
    • min (number): Lower integer boundary.
    • max (number): Upper integer boundary.
  • Returns: (number) An integer between min and max.
var roll = random.int(1, 6)
print("Dice roll: " + roll)
var enemyHp = random.int(50, 100)

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

  • Returns: (number) Floating-point number in [0.0, 1.0).
var chance = random.float()
if (chance < 0.25) {
print("Critical strike!")
}

Selects and returns a random element from the provided array.

  • Parameters:
    • array (array): Non-empty array of values.
  • Returns: (any) A randomly selected item from the array, or null if the array is empty.
var colors = ["red", "green", "blue", "yellow", "purple"]
var picked = random.choice(colors)
print("Chosen color: " + picked)

Randomly shuffles the elements of an array in place using the Fisher-Yates algorithm and returns the array.

  • Parameters:
    • array (array): Array to shuffle.
  • Returns: (array) The shuffled array.
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(deck)
print("Shuffled deck: " + deck)

For convenience, rand(min, max) is also available as a global function alias to random.int(min, max).

var coin = rand(0, 1)
print(coin == 1 ? "Heads" : "Tails")