Returns a pseudo-random number from min up to but not including max; a seed makes the sequence repeatable.
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
math.random(min: series int/float (optional), max: series int/float (optional), seed: series int (optional)) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| min | series int/ | 0.0 | Lower end of the range, included. |
| max | series int/ | 1.0 | Upper end of the range, not reached. |
| seed | series int | na | Starting 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
//@version=6
indicator("Random walk")
step = math.random(-1, 1, 42)
plot(ta.cum(step), "Walk", color = color.blue)See also
Runs on Sidechair