Nice idea. I suggest two minor simplifications:
Simpler syntax using an array literal (
Shortens the string for longer lists.
Additional benefit: explicit type declaration works for any type, not just for
text. Your original idea happens to output
text, because that's the default type for string literals.
ceil() instead of
floor() + 1. Same result.
OK, theoretically, the lower border could be 0 precisely, as hinted in your comment, since
random() produces (quoting the manual here):
random value in the range 0.0 <= x < 1.0
However, I have never seen that happen. Run a couple of million tests:
WHERE ceil(random())::int = 0;
To be perfectly safe, though, you can use Postgres custom array subscripts and still avoid the extra addition:
Details under this related question on SO.
Or better yet, use
trunc(), that's a bit faster.