seed
Builtin index · Statistics & ML
seed(n)
Input: n · Output: no output
What it is
Reseeds the shared random stream with integer n, so subsequent calls to rand/randn/etc. without their own seed= become reproducible from this point on.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
seed(n) | n —not documented yet | no output | without their own seed= become reproducible from this point on. |
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
w = param([0.5, -0.3; 0.2, 0.8])
b = param([0.1, -0.1])
x = [1.0, 2.0; -0.5, 1.5] # two examples, two features each
z = dense(x, w, b)
a = relu(z)
p = sigmoid(a)
loss = sum(p .^ 2)
g = grad(loss, wrt = w)
print("relu: {round(a, 4)}")
print("sigmoid: {round(p, 4)}")
print("dL/dw: {round(g, 4)}")
tape_reset() # clear the tape before the next computation
scores = [2.0, 1.0, 0.1; 0.5, 0.5, 0.5]
probs = softmax_rows(scores)
print("softmax row 0: {round(probs[0, :], 4)}")
seed(3)
kept = dropout(ones(1, 8), 0.5)
print("dropout output: {kept}")
print("survivors: {sum(kept > 0)} of 8, each scaled by 1/(1-rate)")
output
relu: [1, 1.4; 0, 1.25] sigmoid: [0.7311, 0.8022; 0.5, 0.7773] dL/dw: [0.2875, 0.12; 0.5749, 0.9128] softmax row 0: [0.659, 0.2424, 0.0986] dropout output: [2, 2, 0, 0, 2, 0, 2, 0] survivors: 4 of 8, each scaled by 1/(1-rate)
from statistics-ml
How it works
seed(n) reseeds the persistent random stream.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.