// math reference

SQL SETSEED function

Sets the seed for subsequent random() calls in the session, making a random assignment reproducible: setseed(0.5) then random() gives the same series every run.

Kategorie
Math
Returns
void / none
In this category
52 of 82

Syntax

SETSEED(seed)  -- PostgreSQL, seed in [-1, 1]
RAND(seed)  -- SQL Server, MySQL
DBMS_RANDOM.SEED  -- Oracle

Each block is a separate form. The support table and the examples show which engine takes which.

Engine-Unterstützung

EngineSupportNotizen
SQL Server Different name or syntax No SETSEED; RAND(seed) reseeds the connection's generator, so the RAND() calls that follow repeat the same sequence
PostgreSQL Ja setseed(double precision) takes a value between -1 and 1 and applies to the session; usually called via SELECT setseed(0.42)
MySQL Different name or syntax RAND(N) with a constant N is seeded once per statement, so each run of the statement returns the same per-row series
SQLite Not built in random() cannot be seeded; reproducibility is impossible in SQLite
Oracle Different name or syntax DBMS_RANDOM.SEED(value) in PL/SQL, not a plain SQL function

Beispiele

PostgreSQL

SELECT setseed(0.42);
SELECT random() FROM generate_series(1, 3);  -- same three values every run

SQL Server

SELECT RAND(42), RAND(), RAND();  -- the same three values every time this runs

MySQL

SELECT id, RAND(42) AS r FROM t;  -- same per-row values on every run

Gut zu wissen

  • PostgreSQL's setseed takes a double between -1 and 1, not an integer: setseed(42) fails with 'setseed parameter 42 is out of allowed range [-1,1]'.
  • A seed buys reproducibility, not security: knowing the seed predicts every value. Tokens and keys need the crypto functions, seeded or not.
  • SQL Server's RAND() is evaluated once per query regardless; seeding changes which value all rows share, it does not make rows differ.

Questions about SETSEED

How do I make random() reproducible?

Seed it: SELECT setseed(0.42) on PostgreSQL before the random query, RAND(42) on SQL Server or MySQL. SQLite has no seeding at all, so its random() can never be replayed.

Is a seeded random sequence secure?

No. A seed makes the sequence fully predictable: that is the point. Anything security-related needs engine-provided cryptographic bytes, not seeded RAND.

Why does SETSEED affect other sessions?

It does not: the seed is per-session on PostgreSQL and per-connection for RAND() on SQL Server. Other users' random() streams are unaffected. With connection pooling, though, a seed set by one request can carry over to the next request on that connection.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.