How to make real randoms with SQL

How to make real randoms with SQL

Database: PostgreSQL +13

From time to time I need random numbers produced in SQL and packed as an array. This is how it goes:

--PostgreSQL
--Random filled real Arrays:
--Brainstorm:

-- create a function who generates a random real value between low and high:
CREATE OR REPLACE FUNCTION random_real(low real ,high real) 
   RETURNS real AS
$$
BEGIN
   RETURN random()* (high-low + 1) + low;
END;
$$ language 'plpgsql' STRICT;

-- create a function that produce an array of vcount values each between low and high:
CREATE OR REPLACE FUNCTION rvalue_arr(low int ,high int, vcount int) 
   RETURNS setof numeric[] AS
   $$
   select ARRAY_AGG(b.value) as value from (select generate_series(1,(floor(random() * $3)+1)::int),round(random_real($1,$2)::numeric,1) as value) b
   $$
   language sql;

Now you can start a query:

playground=# select rvalue_arr(1,30,20) from generate_series(1,20);
                                        rvalue_arr                                         
-------------------------------------------------------------------------------------------
 {29.3,22.9,1.5,28.4,2.7,28.5,23.7,4.4,23.6,29.2,26.9,1.7}
 {11.8,8.0,6.8,14.3,11.4,21.6,18.5,16.7,27.9,9.2,4.1,10.4,2.8,12.2,3.0,19.4,28.7,7.4,20.9}
 {12.2,1.9,2.4,14.1,5.9,4.5,28.5,4.2,4.0,27.9,7.7,26.9,11.8,2.6,27.4,6.0,23.9,8.6,8.5}
 {28.7,10.0,3.1,5.5,28.0,19.7,18.7,4.4,13.9,17.4,23.5,1.4,10.0,24.9,11.7,3.0}
 {28.1,23.4,8.5,28.2,26.9,6.7,21.9,29.8,26.8,26.9,19.6,25.8,23.5,5.7,11.9,27.0,27.3}
 {10.2,27.3,20.5,5.5,4.2,17.2,13.5,2.3,9.7,16.2,14.7,28.6,27.0,28.0,30.2,7.2}
 {4.8,9.8,30.8,15.6,7.1,16.2,25.6,22.8,22.7}
 {22.8,2.4}
 {11.9,7.2,10.2,14.0,1.5,20.4,23.9,2.8,20.2,4.2}
 {14.1,17.6,24.6,9.4,16.3,8.8,28.0,19.2,6.5,8.7,17.5,21.8,22.4,19.2,25.4,22.0}
 {24.6,5.1,4.1,5.5,13.9,7.8,8.2,2.9}
 {28.1,16.2,3.1,16.9,28.4,11.2,28.6,19.5,21.9,20.3,15.8,18.7,27.5,6.2}
 {26.7,23.5,31.0,28.1,19.2,19.7,25.5}
 {16.0,12.3,24.7,2.3,14.7,29.5,12.1,1.8,10.0,29.0}
 {3.0,15.3,4.0,23.1,15.8,3.6,27.0,3.9,22.7,14.7,26.8,1.7}
 {22.0,4.4,6.8,12.1,21.1,15.5,26.5,26.6,23.1,24.5,28.9,2.7,30.5,21.2,3.4}
 {5.7,30.8,16.6,14.5,6.0,28.2,10.2,3.5,8.3,8.7,29.3,28.3}
 {3.4,17.0,21.0,10.4,7.8,9.2,3.5,29.8,22.7,19.2}
 {6.6,30.6}
 {15.7}
(20 rows)

playground=# select rvalue_arr(1,30,20) from generate_series(1,20);
                                         rvalue_arr                                          
---------------------------------------------------------------------------------------------
 {1.4,8.6,18.8,1.4}
 {10.9}
 {9.4,13.3,9.3,26.7,30.5}
 {13.4,18.5,21.7,30.0,19.7,18.5,25.6,20.7,1.5,23.2,26.7,19.1,24.1}
 {23.5,19.8,6.5,5.2,9.4,21.3,18.6,16.3,13.3,25.5,26.3}
 {6.6,9.2,5.2,21.7,24.7,8.7,14.5,11.0,7.6,3.9,17.4,30.6,16.8,22.4,13.9,13.2,15.0,25.9,23.9}
 {30.9,28.1,20.6,15.6,24.2,25.0,16.0,26.2}
 {20.6,17.9,12.1,15.6,6.7,27.8,26.2,30.6,6.4,16.5,19.1}
 {6.8,12.1,21.4,8.2,22.5}
 {1.9,3.0,20.0,1.3,29.8,15.2,21.7,24.3,6.4,2.7,16.4}
 {5.0,4.3}
 {24.7,10.9,22.4,17.5,6.1}
 {1.7,21.9,30.4,14.5,10.5}
 {12.1}
 {2.7,2.7,26.8,4.3,12.4,20.6,20.4,22.5,9.1,18.9,11.9,7.9,7.5,1.2}
 {21.6,13.1,9.5,15.1,29.4,9.2}
 {5.7,19.7,5.8,18.8,3.9,6.8,27.8,24.6,8.8,26.4,9.2,26.6,2.3,5.5,17.7,9.7,25.9,8.1,26.8}
 {17.4,22.0,5.6,27.3,12.9,23.7,24.5,5.6,25.9,29.2,10.5,8.7,7.5,21.8,20.1,29.1,24.8,1.8,23.8}
 {2.2,5.7,10.0,15.5}
 {10.4,8.9,12.0,20.7,7.3,7.0,27.2,6.5,26.7,4.5,3.9,8.5,7.2,13.0}
(20 rows)

playground=#

Really nice!