Home Explore Blog CI



postgresql

21th chunk of `doc/src/sgml/func.sgml`
1566d75194b1bdb6cb288a75d44d7ea51163417d93d453ba0000000100000fa3
 <parameter>thresholds</parameter> <type>anycompatiblearray</type> )
       <returnvalue>integer</returnvalue>
       </para>
       <para>
        Returns the number of the bucket in
        which <parameter>operand</parameter> falls given an array listing the
        lower bounds of the buckets.  Returns <literal>0</literal> for an
        input less than the first lower
        bound.  <parameter>operand</parameter> and the array elements can be
        of any type having standard comparison operators.
        The <parameter>thresholds</parameter> array <emphasis>must be
        sorted</emphasis>, smallest first, or unexpected results will be
        obtained.
       </para>
       <para>
        <literal>width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])</literal>
        <returnvalue>2</returnvalue>
       </para></entry>
      </row>
     </tbody>
    </tgroup>
   </table>

  <para>
    <xref linkend="functions-math-random-table"/> shows functions for
    generating random numbers.
  </para>

   <table id="functions-math-random-table">
    <title>Random Functions</title>

    <tgroup cols="1">
     <thead>
      <row>
       <entry role="func_table_entry"><para role="func_signature">
        Function
       </para>
       <para>
        Description
       </para>
       <para>
        Example(s)
       </para></entry>
      </row>
     </thead>

     <tbody>
      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>random</primary>
        </indexterm>
        <function>random</function> ( )
        <returnvalue>double precision</returnvalue>
       </para>
       <para>
        Returns a random value in the range 0.0 &lt;= x &lt; 1.0
       </para>
       <para>
        <literal>random()</literal>
        <returnvalue>0.897124072839091</returnvalue>
       </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>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>

         <function>random_normal</function> (
         <optional> <parameter>mean</parameter> <type>double precision</type>
         <optional>, <parameter>stddev</parameter> <type>double precision</type> </optional></optional> )
         <returnvalue>double precision</returnvalue>
       </para>
       <para>
        Returns a random value from the normal distribution with the given
        parameters; <parameter>mean</parameter> defaults to 0.0
        and <parameter>stddev</parameter> defaults to 1.0
       </para>

Title: PostgreSQL Random Number Functions
Summary
This section details the random number functions available in PostgreSQL. It includes `random()`, which returns a random floating-point number between 0.0 and 1.0, as well as overloaded `random()` functions to generate random integers, bigints, or numerics within a specified range. Also described is `random_normal()`, which returns a random number following a normal distribution, with customizable mean and standard deviation.