In 
https://www.postgresql.org/docs/18/functions-math.html#FUNCTIONS-MATH-RANDOM-TABLE
is stated:
random ( ) → double precision
Returns a random value in the range 0.0 <= x < 1.0
random() → 0.897124072839091

random ( min integer, max integer ) → integer
random ( min bigint, max bigint ) → bigint
random ( min numeric, max numeric ) → numeric
Returns a random value in the range min <= x <= max. For type numeric,
the result will have the same number of fractional decimal digits as
min or max, whichever has more.
random(1, 10) → 7
random(-0.499, 0.499) → 0.347

random_normal ( [ mean double precision [, stddev double precision ]]
) → double precision
Returns a random value from the normal distribution with the given
parameters; mean defaults to 0.0 and stddev defaults to 1.0
random_normal(0.0, 1.0) → 0.051285419

The probabilistic distribution used in random() and random(min, max)
is not stated (whereas in random_normal() is).

Google AI pointed me here as the source of the functions:
https://doxygen.postgresql.org/pg__prng_8c_source.html

If this is true (please someone more knowledgeable confirm), the
distribution is stated in the doc in the source as Uniform.

Given this, the "general" doc should reflect this as it is more
accurate and makes it more useful (last IMO).

The attached patch aligns the "general" doc with the "source" doc in this.

There's another random function for dates and related in version 19:
https://www.postgresql.org/docs/19/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
from what I infer from reading here:
https://www.postgresql.org/message-id/flat/2161235.1756479428%40sss.pgh.pa.us#59959fb9783cbc6dcd89699e2c4d4b28
it builds on the random math function and the correction should also
be applied here.

There's another function that relies on random numbers, uuid4 and
gen_random_uuid:
https://www.postgresql.org/docs/18/functions-uuid.html
but this precision can't be made since the random numbers are taken
from external sources.

I hope you find this correct and useful and it can be made part of a
larger patch of minor polishing on docs to be submitted eventually.
Comments are welcome.

Cheers,
Rodrigo
diff --git a/doc/src/sgml/func/func-datetime.sgml b/doc/src/sgml/func/func-datetime.sgml
index 39dddde4fe1..428096ee827 100644
--- a/doc/src/sgml/func/func-datetime.sgml
+++ b/doc/src/sgml/func/func-datetime.sgml
@@ -945,7 +945,7 @@
          <returnvalue>timestamptz</returnvalue>
         </para>
         <para>
-         Returns a random value in the range
+         Returns a random value uniformly from the range
          <parameter>min</parameter> &lt;= x &lt;= <parameter>max</parameter>.
         </para>
         <para>
diff --git a/doc/src/sgml/func/func-math.sgml b/doc/src/sgml/func/func-math.sgml
index 9dcf97e7c9e..078edfb2abd 100644
--- a/doc/src/sgml/func/func-math.sgml
+++ b/doc/src/sgml/func/func-math.sgml
@@ -1065,7 +1065,7 @@
         <returnvalue>double precision</returnvalue>
        </para>
        <para>
-        Returns a random value in the range 0.0 &lt;= x &lt; 1.0
+        Returns a random value uniformly from the range 0.0 &lt;= x &lt; 1.0
        </para>
        <para>
         <literal>random()</literal>
@@ -1090,7 +1090,7 @@
         <returnvalue>numeric</returnvalue>
        </para>
        <para>
-        Returns a random value in the range
+        Returns a random value uniforlmy from 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

Reply via email to