Random Range

From PostgreSQL wiki

Revision as of 18:09, 4 September 2009 by Artacus (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Snippets

Random Range

Works with PostgreSQL

Any version

Written in

SQL

Depends on

Nothing

by Scott Bailey 'Artacus'

CREATE OR REPLACE FUNCTION random(numeric, numeric)
RETURNS numeric AS
$$
   SELECT ($1 + ($2 - $1) * random())::numeric;
$$ LANGUAGE 'sql' VOLATILE;

Usage

SELECT random(1,10)::int, random(1,10);
 
 random     random           
 ---------  ---------------- 
 5          3.20581795740873
Personal tools