Sidechair

math.random()

Browse the reference

Returns a pseudo-random number from min up to but not including max; a seed makes the sequence repeatable.

Try it in the editor

math.random draws a number evenly spread between min and max. Called with no arguments it returns values from 0 up to 1.

Without a seed each run gives different numbers. With a seed, the same sequence comes out on every run, so results can be reproduced and compared.

Syntax

Pine Script
math.random(min: series int/float (optional), max: series int/float (optional), seed: series int (optional)) -> series float

Arguments

NameTypeDefaultDescription
minseries int/float0.0Lower end of the range, included.
maxseries int/float1.0Upper end of the range, not reached.
seedseries intnaStarting value of a repeatable sequence. Leave it out for a different sequence each run.

Returns

series float: a pseudo-random float in the range.

Remarks

  • On Sidechair: a seeded call keeps its generator state as a series, so a live bar that reruns draws the same number again. Without a seed every call draws a fresh number.
  • Not suitable for anything that needs unpredictable numbers.

Example

Pine ScriptA reproducible random walkOpen in editor
//@version=6
indicator("Random walk")
step = math.random(-1, 1, 42)
plot(ta.cum(step), "Walk", color = color.blue)

See also

Runs on Sidechair