Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get unique random numbers?

foxhoundJun 28 2022

I wish to get several random numbers in a range but without duplicates. To do so I could use recursive cte generator as follows:
WITH rand_cte (cnt, value) AS
(
SELECT
1,
ROUND(dbms_random.value(1,20))
FROM dual
UNION ALL
SELECT
cnt + 1,
ROUND(dbms_random.value(1,20))
FROM rand_cte
WHERE cnt < 5
)

SELECT value
FROM rand_cte;

But such a solution will include duplicates as well. Any ideas how to filter them out in some way? Maybe you know different solution?

This post has been answered by Frank Kulash on Jun 28 2022
Jump to Answer
Comments
Post Details
Added on Jun 28 2022
3 comments
466 views