I would strongly advise you to use the most appropriate data type possible - if the data is integers, then use an integer type!
You want PostgreSQL's generate_series
This code will generate as many random integers between 0 and 20 as you want.
SELECT (20 * random())::INTEGER AS rand FROM generate_series(1, 10);
Result -
rand
0
15
2
10
14
14
10
0
18
18
See the fiddle here! You can vary the second parameter to whatever value suits you!
To generate random text, you can use this code:
SELECT MD5(random()::TEXT) FROM GENERATE_SERIES(1, 5);
Result:
md5
de5161e6ed7492d046d02bfc5c166c86
a8818e4e472c4a8a923abf2c9e97a70c
398300e6a10e4c493b64403adeeac3c7
bf548d3db7763e11ee05e41ab0f2b3d9
c914d60c26a5c75fa794ef119ed70c10
To generate text only, you can use the REPLACE
and/or the TRANSLATE
functions to change the MD5 numeric data to the empty string. Lots of good "tricks" to be found here, here and here.
The best though would be the REGEXP_REPLACE
function. You could do something like this:
WITH cte AS
(
SELECT MD5(random()::TEXT) AS rand FROM GENERATE_SERIES(1, 5)
)
SELECT REGEXP_REPLACE(rand,'[[:digit:]]','','g') AS letters FROM cte
Result:
letters
addebafaeb
ccdfaddbdccadc
debdbdfabefbeff
debdaecabef
ebfbaeefdbc
p.s. welcome to the forum!