Hello,
I am trying to randomly sample x amount of rows from a CDT by generating x random numbers in the range 1:length(myCdt). The issue I am getting is that some of these random numbers are repeated.
Is there a way to generate random numbers in a given interval and have them be unique?
Here is what I have so far:
local!randomNumbers:if(
length(local!query)>15, 1+ tointeger(rand(15) * length(local!query)), local!query,
),
Discussion posts and replies are publicly visible
Without using a recursive approach, this is tricky. The most simple approach might be, to just generate more numbers, get the unique ones and then take the first few. Find a code example below.
index( union(tointeger(rand(100)*50), tointeger({})), enumerate(15) + 1, {} )
I see, is there a limit to how high that number should be?
Hm... as this is random, it could, in theory, generate a thousand times the same number. In practice, you could double it.
In my tests, when generating 50 numbers, I had 0-3 double. When you need 15, you should be good with 20. If your application breaks, in case you only show 13 or 14 items, go a bit higher.
Makes sense, I think using a multiple of the length of my cdt might be a good way of choosing that limit