Postgresql Setseed Function

PostgreSQL: setseed Function

In this PostgreSQL post explains how to use the PostgreSQL setseed function with syntax and examples.

Description

The PostgreSQL setseed function can be used to set a seed for the next time that you call the random function. If you do not call setseed, PostgreSQL will use its own seed value. This may or may not be truly random.

If you set the seed by calling the setseed function, then the random function will return a repeatable sequence of random numbers that is derived from the seed.

TIP: The setseed function can be very useful in situations where you want to ensure that PostgreSQL uses a truly random seed.

Syntax

The syntax for the setseed function in PostgreSQL is:

setseed( seed )

Parameters or Arguments

seed

A value between 1.0 and -1.0, inclusive, that is used to provide the seed for the next call to the random function.

Note

  • The setseed function does not return a value. It is merely a function used to seed the random function.
  • See also the random function.

Applies To

The setseed function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example

Let's explore how to use the setseed function in PostgreSQL to seed the random function.

Calling the Random Function Without a Seed

If you do not call the setseed function before the random function, PostgreSQL will provide its own seed. This value will not be known to you.

For example:

postgres=# SELECT random();
      random
-------------------

 0.623784058727324
(1 row)

postgres=# SELECT random();
      random       
-------------------

 0.828258865978569
(1 row)

postgres=# SELECT random();
random       
-------------------

 0.269835902377963
(1 row)

Each time you execute the random function, you will get a new random number. Your random numbers returned will differ from our example above because your seed will be different from ours.

Calling the Random Function With a Seed

Now, let's look at what happens when we use the setseed function before calling the random function.

For example, let's set the seed to 0.5 and then call the random function 3 times:

postgres=# SELECT setseed(0.5);
 setseed 
---------


(1 row)

postgres=# SELECT random();
      random
-------------------

 0.798512778244913
(1 row)

postgres=# SELECT random();
      random
-------------------

 0.518533017486334
(1 row)

postgres=# SELECT random();
      random
-------------------

 0.0734698106534779
(1 row)

In this example, we call setseed once followed by the random function 3 times. By doing this, we get predictable random numbers. With a seed of 0.5, you will always get 0.798512778244913 as the first random number, 0.518533017486334 as the second random number, and 0.0734698106534779 as the third random number.

Changing the Seed In Between Calls to Random

Sometimes it is desirable to never have predictable results. This can be achieved by calling setseed with different values in between calls to the random function.

For example:

postgres=# SELECT setseed(0.5);
 setseed 
---------


(1 row)

postgres=# SELECT random();
      random
-------------------

 0.798512778244913
(1 row)

postgres=# SELECT setseed(0.38);
 setseed 
---------


(1 row)

postgres=# SELECT random();
      random
-------------------

 0.771267373114824
(1 row)

postgres=# SELECT setseed(-0.21);
 setseed 
---------


(1 row)

postgres=# SELECT random();
      random
-------------------

 0.590676144231111
(1 row)

By calling setseed between calls to the random function, the random numbers will not be predictable.