Skip to content
Plugins (190)

random

stable

Pseudo-random number generation using a Xorshift64 PRNG, with functions for floats, integers, booleans, collections, UUIDs, dice rolls, and normal distribution sampling.

use plugin random
13 functions Math
Runs with the native Zolo host

Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.

/ filter jk navigate Esc clear
Functions (13)
  1. float Random float in [0, 1)
  2. int Random integer in [min, max]
  3. bool Random boolean value
  4. range Random float in [min, max]
  5. choice Pick a random element from a table
  6. shuffle Return a shuffled copy of a table
  7. sample Pick n unique elements without replacement
  8. bytes Generate n random bytes
  9. uuid_v4 Generate a random UUID v4 string
  10. seed Seed the global PRNG
  11. weighted_choice Pick an element by weight
  12. dice Roll one or more dice
  13. normal Sample from a normal distribution

From import to result

  1. 01Import only what you use
  2. 02Copy the smallest example
  3. 03Run it with the Zolo CLI

Random float in [0, 1)

Returns a random floating-point number in the range [0, 1).

use plugin random::{float}

let r = float()
print(r)  // e.g. 0.7312...

Random integer in [min, max]

Returns a random integer in the inclusive range [min, max].

use plugin random::{int}

let roll = int(1, 6)
print("rolled {roll}")

Random boolean value

Returns either true or false with equal probability.

use plugin random::{bool}

if bool() {
  print("heads")
} else {
  print("tails")
}

Random float in [min, max]

Returns a random float uniformly distributed in [min, max].

use plugin random::{range}

let angle = range(0.0, 360.0)
let temp = range(-10.0, 40.0)

Pick a random element from a table

Picks and returns one random element from a table. Errors if the table is empty.

use plugin random::{choice}

let fruits = ["apple", "banana", "cherry"]
print(choice(fruits))

Return a shuffled copy of a table

Returns a new table with the same elements in a random order (Fisher-Yates). The original table is not modified.

use plugin random::{shuffle}

let deck = [1, 2, 3, 4, 5, 6, 7, 8]
let shuffled = shuffle(deck)
print(shuffled[1])

Pick n unique elements without replacement

Returns a new table of n unique elements chosen at random from the source table, without replacement. Errors if n exceeds the table length.

use plugin random::{sample}

let players = ["Alice", "Bob", "Carol", "Dave", "Eve"]
let team = sample(players, 3)
print(team[1])

Generate n random bytes

Generates n random bytes. Maximum is 1 MB (1,048,576 bytes).

use plugin random::{bytes}

let token = bytes(32)

Generate a random UUID v4 string

Generates a random RFC 4122 UUID v4 string in the standard xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx format.

use plugin random::{uuid_v4}

let id = uuid_v4()
print(id)  // e.g. "550e8400-e29b-41d4-a716-446655440000"

Seed the global PRNG

Seeds the global PRNG with a specific integer value. Use this to make random sequences reproducible in tests.

use plugin random::{seed, int}

seed(42)
print(int(1, 100))  // always the same value

Pick an element by weight

Picks an element by weight. The input table must contain entries of the form #{value, weight}. Higher weight means higher probability.

use plugin random::{weighted_choice}

let item = weighted_choice([
  #{"value": "common",   "weight": 70},
  #{"value": "rare",     "weight": 25},
  #{"value": "legendary","weight": 5}
])
print(item)

Roll one or more dice

Rolls count dice each with sides faces (default: 1d6). Returns a single integer when count is 1, or a table of results otherwise.

use plugin random::{dice}

print(dice())        // 1d6
print(dice(20))      // 1d20
let rolls = dice(6, 4)  // 4d6
for _, v in rolls { print(v) }

Sample from a normal distribution

Samples from a normal (Gaussian) distribution using the Box-Muller transform. Defaults to mean 0.0 and standard deviation 1.0.

use plugin random::{normal}

let height = normal(170.0, 10.0)  // mean 170cm, stddev 10cm
print(height)

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en