Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Random Functions

Functions for generating random values: numbers, strings, and selections.

Summary

FunctionSignatureDescription
random-> numberGenerate random number between 0 and 1
samplearray, number -> arrayRandom sample from array
shufflearray -> arrayRandomly shuffle array

Functions

random

Generate random number between 0 and 1

Signature: -> number

Examples:

# Random float
random() -> 0.123456...
# Random 0-99
floor(multiply(random(), `100`)) -> 42

CLI Usage:

echo '{}' | jpx 'random()'

sample

Random sample from array

Signature: array, number -> array

Examples:

# Sample 2 items
sample([1, 2, 3, 4], `2`) -> [3, 1]
# Sample 1 item
sample(['a', 'b', 'c'], `1`) -> ['b']
# Always returns n items
length(sample([1, 2, 3], `2`)) -> 2

CLI Usage:

echo '{}' | jpx 'sample([1, 2, 3, 4], `2`)'

shuffle

Randomly shuffle array

Signature: array -> array

Examples:

# Shuffle numbers
shuffle([1, 2, 3]) -> [2, 3, 1]
# Preserves length
length(shuffle([1, 2, 3])) -> 3

CLI Usage:

echo '{}' | jpx 'shuffle([1, 2, 3])'