[solved] How to randomly select certain number of keywords from a large list of keywords?

I have a text file with a list of keywords. I would like to select specific number of keywords (say, 5) from the list.

Each time I run the macro, it should automatically pick 5 random keywords from the list. How to do that?

A true random list might have some duplicate lines. But assuming you want 5 different random lines in the list... here is a way to do that with Keyboard Maestro.

It picks a random line and adds that to a Variable. Then it picks another random line and if that is not already in the Variable it gets added. It keeps doing this until the Variable is 5 lines.

To return more or less than 5 lines, change the number in the first Action.

EXAMPLE Display Five Random Lines from List v2.00.kmmacros (8.2 KB)

Click to Show Image of Macro


I am not being able to open your macro.

Screenshot 2023-07-26 at 09.41.56

I've uploaded it again as version 2.00 in original post. Should import fine now.

1 Like

Thank you so much

Could you explain what the "1" mean here?

That number is only a preview. As it is showing the value of a local variable it won’t update to the true number of lines until the Macro has run. At the end of the Macro run it will not retain any value as local variables only last while the macro is still running.

It is showing “1” probably because 1 is the minimum number of lines any variable can have.

Hope the makes sense!

Your explanation makes sense :).

My follow up question is, how come that particular local variable is not showing the true value for preview during or after the macro is run?

Below is a screenshot of my test macro which shows the true values for all local variables, except for Local__CharCount. :confused:

Because Local__CharCount needs to get its value by filtering the lines from Local__Text and until the Macro is run Local__Text doesn’t exist.

All the other previews are from things Keyboard Maestro can get the values for before the Macro is run.

1 Like

For some reason, I thought the preview values were the last values stored in the variables, like the system clipboard. But the previews are values that KM can obtain in real time without running the macro.

Thank you for the detailed explanation. I think I finally got it now. :slight_smile:

If the Variables are Global Variables (i.e. named without Local or Instance at their front) then their values will be retained after the Macro is run and the preview will indeed show their last stored value…

1 Like

That explains why I was confused about the previews of local variables.

1 Like

Here's another method that also uses Keyboard Maestro native Actions. I think it might be simpler to understand what is going on than in the previous method I uploaded.

It uses the Keyboard Maestro Filter Action to "shuffle" all the lines of the original list and from there returns the first 5 lines (or however many you want).

EXAMPLE Display Five Random Lines from List v3.00.kmmacros (7.1 KB)

Click to Show Image of Macro

1 Like