1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-25 13:17:41 +03:00

Add date and timestamp variants of random(min, max).

This adds 3 new variants of the random() function:

    random(min date, max date) returns date
    random(min timestamp, max timestamp) returns timestamp
    random(min timestamptz, max timestamptz) returns timestamptz

Each returns a random value x in the range min <= x <= max.

Author: Damien Clochard <damien@dalibo.info>
Reviewed-by: Greg Sabino Mullane <htamfids@gmail.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Vik Fearing <vik@postgresfriends.org>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/f524d8cab5914613d9e624d9ce177d3d@dalibo.info
This commit is contained in:
Dean Rasheed
2025-09-09 10:39:30 +01:00
parent 5ac3c1ac22
commit faf071b553
7 changed files with 254 additions and 10 deletions

View File

@@ -928,6 +928,36 @@
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>random</primary>
</indexterm>
<function>random</function> ( <parameter>min</parameter> <type>date</type>, <parameter>max</parameter> <type>date</type> )
<returnvalue>date</returnvalue>
</para>
<para role="func_signature">
<function>random</function> ( <parameter>min</parameter> <type>timestamp</type>, <parameter>max</parameter> <type>timestamp</type> )
<returnvalue>timestamp</returnvalue>
</para>
<para role="func_signature">
<function>random</function> ( <parameter>min</parameter> <type>timestamptz</type>, <parameter>max</parameter> <type>timestamptz</type> )
<returnvalue>timestamptz</returnvalue>
</para>
<para>
Returns a random value in the range
<parameter>min</parameter> &lt;= x &lt;= <parameter>max</parameter>.
</para>
<para>
<literal>random('1979-02-08'::date,'2025-07-03'::date)</literal>
<returnvalue>1983-04-21</returnvalue>
</para>
<para>
<literal>random('2000-01-01'::timestamptz, now())</literal>
<returnvalue>2015-09-27 09:11:33.732707+00</returnvalue>
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>

View File

@@ -1151,7 +1151,8 @@
<para>
The <function>random()</function> and <function>random_normal()</function>
functions listed in <xref linkend="functions-math-random-table"/> use a
functions listed in <xref linkend="functions-math-random-table"/> and
<xref linkend="functions-datetime-table"/> use a
deterministic pseudo-random number generator.
It is fast but not suitable for cryptographic
applications; see the <xref linkend="pgcrypto"/> module for a more