Skip to content
Advertisement

Generate random numbers in a specific range without duplicate values

This creates random numbers. However, it spits out duplicates

Advertisement

Answer

In the comments to my other answer, you write:

The table I’m working with has an ID , Name , and I want to generate a 3rd column that assigns a unique random number between 1-275 (as there are 275 rows) with no duplicates.

In the future, please include details like this in your original question. With this information, we can help you out better.

First, let’s make a sample of your problem. We’ll simplify it to just 5 rows, not 275:

Let’s now add the third column you want:

Finally, let’s update the table by creating a subquery that orders the rows randomly using row_number(), and applying the output the the column we just created:

Here’s the result I get, but of course it will be different every time it is run:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement