Randomly select a row when pressing a button?

Saw this question in our support channel and thought it might be worth sharing here.

@Moriah has a solution to this one -

There is a way to do this! The formula for it is a bit complex at the moment so I’ll explain it to you. To select a random row from a table, you would have a formula that looks something like this (where tableName is the name of your table):

tableName.nth((random() * tableName.count()).roundUp(0))

Here’s how it works:

  • random() grabs a random decimal number between 0 and 1
  • multiply this by the number of rows in the table (we find this using tableName.count() ), and then round up to the nearest integer so that you end up with a random integer number from 1 to the size of the table, representing a random row
  • tableName.nth() looks up a row based on its row number. So, we insert the random number we found into the nth() formula to get a random row.

If you wanted to, say, show the text of a random row when pressing a button, you might do something like:

  • On button press, modify the row of a table to hold the result with the row lookup we used here! then you can grab the data off of that row there. Something like this:

8 Likes

With RandomInteger():

[table].nth(RandomInteger(0, [table].count())

3 Likes