Qu

rand

Builtin index · Statistics & ML

result = rand([rows], [cols], [seed=])

Input: rows, cols, seed · Output: result

What it is

Draws from the uniform distribution on [0, 1). rows/cols are integers, both defaulting to 1, giving the output shape (a number if both are 1, otherwise a vector or (rows, cols) matrix); an optional integer seed= starts a fresh reproducible stream, otherwise the shared stream advances.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = rand([rows], [cols], [seed=])
rows number, vector, or matrix, optionalrows/cols are integers, both defaulting to 1, giving the output shape (a number if both are 1, otherwise a vector or (rows, cols) matrix).
cols number, vector, or matrix, optionalrows/cols are integers, both defaulting to 1, giving the output shape (a number if both are 1, otherwise a vector or (rows, cols) matrix).
seed optionalan optional integer seed= starts a fresh reproducible stream, otherwise the shared stream advances.
result scalar, Num, Vec, or Mat
Returns a scalar Num when rows and cols are both 1, a Vec when one of them is, and a (rows, cols) Mat otherwise.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

seed(42)
a = rand(1, 3)
seed(42)
b = rand(1, 3)
print("a = {a}")
print("b = {b}")

from repl-diagnostics

a = rand(3)
print("shape a: {size(a)}")

seed(5)
p = rand(2, 2)
junk = rand(2, 2, seed=42)
q = rand(2, 2)

seed(5)
p2 = rand(2, 2)
q2 = rand(2, 2)

print("q == q2: {q == q2}")
output
shape a: [3, 1]
q == q2: [1, 1; 1, 1]

from repl-diagnostics

seed(42)                             # reseed the shared stream

u = rand(3, seed = 1)                # an explicit seed still wins

print("rand draws: {round(u, 3)}")
print("chi2pdf(2, 3):  {round(chi2pdf(2, 3), 4)}")
print("normpdf(0):     {round(normpdf(0), 4)}")

walk = random_walk(20, drift = 0.1, volatility = 1.0, seed = 3)
print("random walk, last value: {round(walk[19], 3)}")
output
rand draws: [0.746, 0.971, 0.444]
chi2pdf(2, 3):  0.2076
normpdf(0):     0.3989
random walk, last value: -3.552

from statistics-ml

How it works

rand/randn draw rows*cols samples. An explicit seed= gives a fresh reproducible local stream; otherwise the persistent stream advances so successive draws differ.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.