Random
Random in ProBuilder generates a random integer between two inclusive limits. Useful for test data, simulations, and randomized values in ProRealTime™ code.
Syntax
value = Random(limit1, limit2)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
limit1 | integer | required | One boundary of the range, inclusive. |
limit2 | integer | required | The other boundary of the range, inclusive. |
How it works
Random draws an integer from the range defined by its two limits, both boundaries included. The order of the arguments does not matter, Random(-100, 1000) and Random(1000, -100) describe the same range, the function sorts out which limit is the lower bound.
Each evaluation produces a new draw. Since a ProBuilder script runs once per bar, a Random call inside the main body yields a different value on every bar, and calling it twice on the same bar yields two independent values. There is no seed control, so results are not reproducible between runs, two backtests over identical data can produce different sequences.
The function returns integers only. To simulate a decimal draw, generate a larger integer range and divide, for example Random(0, 1000) / 1000 approximates a uniform value between 0 and 1.
Random has no predictive content. Its practical uses are generating synthetic series while developing display code, adding jitter to test data, and building random baselines against which a rule-based system can be sanity-checked in a backtest.
Examples
Example 1, Generating a random integer (Indicator)
// Draw a new integer between -100 and 1000 on each bar
a = Random(-100, 1000)
RETURN aEach bar plots a fresh draw from the inclusive range, producing a noise series useful for testing display and scaling logic.
Example 2, Random entry baseline for comparison (ProBacktest)
// Coin-flip entries with fixed exits, used as a neutral baseline
// against which a real entry rule can be compared
IF NOT onmarket THEN
coin = Random(0, 1)
IF coin = 1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
ENDIF
SET STOP %LOSS 1
SET TARGET %PROFIT 1Random supplies a 50/50 entry signal. Comparing a strategy against this baseline shows how much of its result comes from the entry logic rather than the exit settings.
Example 3, Simulated random walk series (Indicator)
// Build a cumulative random walk for testing chart code
ONCE walk = 0
walk = walk + Random(-10, 10)
RETURN walk AS "random walk"Each bar adds a random step between -10 and 10 to the running total, producing a synthetic price-like series without loading real data.
Common errors and gotchas
- Integers only. Random never returns decimals.
Random(0, 1)yields only 0 or 1, not values in between. Divide a wider integer draw to approximate decimal ranges. - New value on every evaluation. Two calls on the same bar return independent draws. Store the result in a variable when the same draw must be reused within a bar.
- No reproducibility. There is no seed parameter, so backtests containing Random are not repeatable. Identical code and data can produce different equity curves on each run.
- Recalculation changes history. When an indicator recalculates, past bars draw new values, so a Random-based plot redraws its own history and cannot be treated as a stable series.
Related instructions
ROUND, rounds a decimal value to the nearest integer.FLOOR, rounds a value down to the nearest integer.CEIL, rounds a value up to the nearest integer.ABS, absolute value of a number.MAX, larger of two values, useful for clamping draws.MIN, smaller of two values, useful for clamping draws.MOD, remainder of an integer division.SGN, sign of a value as -1, 0, or 1.
