SETSEED

Set the seed for the pseudo-random number generator used by RANDOM().

Category: numericReturns: DOUBLEDialect: PostgreSql

Syntax

SETSEED(seed)

Description

## Overview Sets the seed of the session-local pseudo-random number generator used by RANDOM() and related functions. Once set, subsequent RANDOM() calls produce a deterministic sequence: the same seed always produces the same sequence until the next SETSEED or session end. SETSEED is the mechanism for reproducibility in tests and demonstrations: set the seed before a query that uses RANDOM(), and the output is stable. The seed is session-local and does not propagate across connections. ## Behavior - Accepts a single DOUBLE argument in the closed interval [-1.0, 1.0]. - Raises an error for inputs outside the valid range. - Returns void (no result set). Some engines return NULL as a placeholder. - Affects RANDOM() and any other function that shares the session's RNG state. - The seed persists for the remainder of the session unless overwritten. ## Numeric precision - The seed is interpreted as a 32-bit signed integer derived from the DOUBLE, so the effective seed space is coarser than the full DOUBLE precision. - Two seeds that round to the same underlying integer produce identical sequences. ## Compatibility - Matches the PostgreSQL SETSEED function, including the [-1.0, 1.0] input range. - Not a SQL standard function; other engines expose similar reseed primitives under different names.

Parameters

NameTypeDescription
seedSpecifies the seed value. Must be a DOUBLE in the closed interval [-1.0, 1.0]. Inputs outside this range raise an error.

Examples

-- Set a specific seed for reproducible results
SELECT SETSEED(0.42);
-- Set seed then generate a random number
SELECT SETSEED(0.5);
SELECT RANDOM();
-- Seed of zero
SELECT SETSEED(0.0);
-- Negative seed
SELECT SETSEED(-0.75);
-- Reseed inside a stored procedure or session to make a downstream SELECT deterministic
SELECT SETSEED(0.123);
SELECT trade_id, RANDOM() AS jitter
FROM finance.trades.orders
LIMIT 5;

Pitfalls

Open in interactive docs →   DeltaForge home →