Random sorting

As I remember, FQL does not provide a random function for security reasons. But it should be possible to provide some random value from the client code and sort by that. Am I correct?

How to sort ‘randomly’

I think you are correct in your assumption. The way to sort documents randomly could be to store a random value via your app’s code (JS, C#, …) e.g.

client.query(Create(
    Collection('posts'),
    { data: { 
        title: 'something interesting...',
        sortvalue: Math.random()
   } },
  )
)

And then create an index on this sort value.

CreateIndex({
    name: 'random-index',
    source: Collection('posts'),
    values: [{ field: ['data', 'sortvalue'] }],
  })

Of course, the random ordering will no longer change and always be the same random ordering.
You could get creative with that if you want and either over time change these values or provide multiple random values:

client.query(Create(
    Collection('posts'),
    { data: { 
        title: 'something interesting...',
        sortvalues: {
           v1: Math.random(),
           v2: Math.random()
        }
   } },
  )
)

And then create multiple indexes and pick your index randomly :wink:

Now I just realized, that for many cases it should be possible and good enough to use Pseudorandom number generator

function random(seed: number) {
  const nextSeed = (1839567234 * seed + 972348567) % 8239451023
  return nextSeed;
}

const seed = 1;
const randomNumber1 = random(seed);
const randomNumber2 = random(randomNumber1);