Random Range

From PostgreSQL wiki
Jump to navigationJump to 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