Skip to content
Advertisement

How to select a fixed number of random rows based on a repeatable seeding?

I’m struggling on how can I fix, for example, 5 random rows of my table in SQL. But I need to be able to repeatable retrieve the same 5 rows every time I run the code with a given seed.

I tried to use

But I didn’t get the same sample each time that I run the code.

Any idea on how can I do that?

Advertisement

Answer

The RAND function can have a seed as parameter.

But the resulting “random” float is predictable.
F.e.

would give the same order as

So the seeds need to be spiced up.
For example by a calculating a hash.

 id | col1
--: | ---:
164 |   64
200 |  100
188 |   88
150 |   50
106 |    6
 id | col1
--: | ---:
164 |   64
200 |  100
188 |   88
150 |   50
106 |    6
 id | col1
--: | ---:
111 |   11
112 |   12
117 |   17
180 |   80
150 |   50

db<>fiddle here

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