I’m building a macro where I need Keyboard Maestro to check 3 random boxes out of 11 every time it runs.
I know about the “Set Variable to Calculation” action, but I can’t get `Random(1,11)' to work.
I also tried using %Random%1,11% but I’m not sure how to make sure the three numbers are unique (no repeats).
Ideally, I’d like to store the three random values in variables (Box1, Box2, Box3) and then use If/Then actions to map those numbers to the correct checkboxes.
Could anyone show me the simplest way to generate 3 unique random numbers between 1–11
and then act on them?
Welcome to the forum! I love problems like this, I know there are probably MANY ways to accomplish this, but one way to do it, is instead of using the random function, you could create a list of exactly 3 1's, and the rest 0's, and use the filter action to shuffle that list, and use the list as an array to set the checkbox state in the prompt.
There will be many solutions. Simple solutions may depend upon certain things that you didn't specify, like whether the 11 boxes will always be in the exact same location on the screen. If not, then the simplest solution may depend upon other images on the screen, but your screen capture only included the list of boxes, and it could help a lot if we could see what's further away from the boxes.
In one approach, which will generate three random numbers from 1 to 11, you don't just want three random numbers from 1 to 11, but you actually want three random DIFFERENT numbers from 1 to 11. If they weren't different, that would be a "bug" for you. Technically then, because they are different numbers, they aren't INDEPENDENTLY random. You want three different numbers from a set of 11. This is easy enough to solve, but I'm not sure if I have the shortest solution to that problem. My solution has two steps: (a) Generate three different random numbers from 1 to 11, (b) Use those three numbers to calculate click locations in a loop and click on those locations. With a brief pause, to be safe.
Of course, for this to work you need to know the location of the boxes, and you need to adjust the values of 200, 300 and 25. (200 and 300 are the X,Y locations of the boxes, and 25 is the exact number of pixels between each row, which may be a fractional value.)
Does this help? Let me know if something isn't quite right. I do this sort of thing regularly. I do it so often that I didn't even test this code, because I'm pretty sure I got it right.
Here's another completely different solution that has certain advantages. It collects the different locations of all the eligible boxes in a list, then randomizes the list, then takes the first three items, then click in those locations. Quite slick.
This is awesome! Thank you. The only thing I had to change was "jot 11 0" because the starting point of the first box could be multiplied by 0 instead of 1 which offset the selections by one box.
These are good general methods. But I do like @Airy's "find the boxes" way of solving this particular problem -- maybe swapping out the "Execute Shell Script" action for:
Thanks. I use macros that do this every day. In fact, I give higher probability to certain locations on the screen by adding multiple copies of the %FoundImage% data to the “accumulation variable” (which, in my example, is LocalList).