Generate RANDOM and UNIQUE numbers for a specific range

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

Parents
  • There might be a simpler way to do this, but this solution should guarantee a random order without repeated data regardless of how many values you have:

    a!localVariables(
      local!numberOfValues: 15,
      local!randomData: rand(local!numberOfValues),
      local!index: a!forEach(
        items: local!randomData,
        expression: rank(
          fv!item,
          local!randomData
        )
      ),
      index(
        enumerate(local!numberOfValues)+1,
        local!index
      )
    )

    Basically you just generate a list of random decimals and then find the rank in the list of each of them. That will guarantee that all values are used and that they don't repeat!

Reply
  • There might be a simpler way to do this, but this solution should guarantee a random order without repeated data regardless of how many values you have:

    a!localVariables(
      local!numberOfValues: 15,
      local!randomData: rand(local!numberOfValues),
      local!index: a!forEach(
        items: local!randomData,
        expression: rank(
          fv!item,
          local!randomData
        )
      ),
      index(
        enumerate(local!numberOfValues)+1,
        local!index
      )
    )

    Basically you just generate a list of random decimals and then find the rank in the list of each of them. That will guarantee that all values are used and that they don't repeat!

Children
No Data