website is under construction
Standard Library

Random

import "ghost:random"
import { random, seed } from "ghost:random"

The random module generates random numbers.

This module and math share one generator. Seeding either one seeds both, so a seeded run replays exactly however you ask for your numbers.

Methods

random.random()

Returns a random decimal. With no arguments the result falls in [0, 1):

random.random()

With one argument, that argument is the maximum and the minimum is zero:

random.random(10) // between 0 and 10

With two, they are the minimum and the maximum:

random.random(5, 10) // between 5 and 10

The result is always a decimal. For a whole number, use math.randomInt(), which draws from the same generator and is the one call that always answers a whole number — that is why the two are named apart.

random.seed()

Initializes the random number generator. Pass a seed value to make a run reproducible; pass nothing and the current time is used.

random.seed(42)

math.randomSeed(n) is the same operation under the math module's name.

Properties

random.currentSeed

The seed currently driving the generator.

random.seed(42)

console.log(random.currentSeed) // >> 42
Reading the seed and setting it are deliberately two different words: random.currentSeed is the property, random.seed() is the method. A getter and its setter never share a name in Ghost, so which one a piece of code means is never a guess from context.