aboutsummaryrefslogtreecommitdiff
path: root/doc/src
diff options
context:
space:
mode:
authorDean Rasheed <dean.a.rasheed@gmail.com>2024-03-27 10:12:39 +0000
committerDean Rasheed <dean.a.rasheed@gmail.com>2024-03-27 10:12:39 +0000
commite6341323a8da64b18e9af3e75a4578230702d61c (patch)
treef04f8e7fa84af4b569e58c85d2a7d98f65f45303 /doc/src
parent818861eb578663a0d4d8d7dc4e18c96a148b3c75 (diff)
downloadpostgresql-e6341323a8da64b18e9af3e75a4578230702d61c.tar.gz
postgresql-e6341323a8da64b18e9af3e75a4578230702d61c.zip
Add functions to generate random numbers in a specified range.
This adds 3 new variants of the random() function: random(min integer, max integer) returns integer random(min bigint, max bigint) returns bigint random(min numeric, max numeric) returns numeric Each returns a random number x in the range min <= x <= max. For the numeric function, the number of digits after the decimal point is equal to the number of digits that "min" or "max" has after the decimal point, whichever has more. The main entry points for these functions are in a new C source file. The existing random(), random_normal(), and setseed() functions are moved there too, so that they can all share the same PRNG state, which is kept private to that file. Dean Rasheed, reviewed by Jian He, David Zhang, Aleksander Alekseev, and Tomas Vondra. Discussion: https://postgr.es/m/CAEZATCV89Vxuq93xQdmc0t-0Y2zeeNQTdsjbmV7dyFBPykbV4Q@mail.gmail.com
Diffstat (limited to 'doc/src')
-rw-r--r--doc/src/sgml/func.sgml43
1 files changed, 38 insertions, 5 deletions
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 8ecc02f2b90..93b0bc2bc6e 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -1865,6 +1865,39 @@ SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
+ <primary>random</primary>
+ </indexterm>
+ <function>random</function> ( <parameter>min</parameter> <type>integer</type>, <parameter>max</parameter> <type>integer</type> )
+ <returnvalue>integer</returnvalue>
+ </para>
+ <para role="func_signature">
+ <function>random</function> ( <parameter>min</parameter> <type>bigint</type>, <parameter>max</parameter> <type>bigint</type> )
+ <returnvalue>bigint</returnvalue>
+ </para>
+ <para role="func_signature">
+ <function>random</function> ( <parameter>min</parameter> <type>numeric</type>, <parameter>max</parameter> <type>numeric</type> )
+ <returnvalue>numeric</returnvalue>
+ </para>
+ <para>
+ Returns a random value in the range
+ <parameter>min</parameter> &lt;= x &lt;= <parameter>max</parameter>.
+ For type <type>numeric</type>, the result will have the same number of
+ fractional decimal digits as <parameter>min</parameter> or
+ <parameter>max</parameter>, whichever has more.
+ </para>
+ <para>
+ <literal>random(1, 10)</literal>
+ <returnvalue>7</returnvalue>
+ </para>
+ <para>
+ <literal>random(-0.499, 0.499)</literal>
+ <returnvalue>0.347</returnvalue>
+ </para></entry>
+ </row>
+
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
<primary>random_normal</primary>
</indexterm>
@@ -1906,19 +1939,19 @@ SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in
</table>
<para>
- The <function>random()</function> function uses a deterministic
- pseudo-random number generator.
+ The <function>random()</function> and <function>random_normal()</function>
+ functions listed in <xref linkend="functions-math-random-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
secure alternative.
If <function>setseed()</function> is called, the series of results of
- subsequent <function>random()</function> calls in the current session
+ subsequent calls to these functions in the current session
can be repeated by re-issuing <function>setseed()</function> with the same
argument.
Without any prior <function>setseed()</function> call in the same
- session, the first <function>random()</function> call obtains a seed
+ session, the first call to any of these functions obtains a seed
from a platform-dependent source of random bits.
- These remarks hold equally for <function>random_normal()</function>.
</para>
<para>