random pick 3 squares in grid Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chango
Posts: 30
Joined: 27 May 2017, 12:10

random pick 3 squares in grid

07 Jun 2022, 15:14

So I have a 3 x 3 grid of squares - buttons - pictures etc.
I want to click 3 of the squares randomly
I know how to do picture search and move and click,
I just cant seem to work out how or where to start with the logic.
Also how to turn say, a random number into executing a particular action, i.e move then click.
Any hlp much appreciated as ever :) :?:
User avatar
Chunjee
Posts: 1501
Joined: 18 Apr 2014, 19:05
Contact:

Re: random pick 3 squares in grid

07 Jun 2022, 23:22

I apologize this is probably the most advanced way to handle it; but you will learn objects on the way :) and I also think it is the most scalable to click more locations.

If we save one set of coordinates and click on them like so:

Code: Select all

setOfCoords := {x: 100, y: 333}
click, % setOfCoords.x " " setOfCoords.y
Then we can make an array of coordinates like so and choose randomly from it:

Code: Select all

arrayOfCoords := [{x: 100, y: 333}
				, {x: 200, y: 333}
				, {x: 300, y: 999}
				, {x: 400, y: 333}
				, {x: 500, y: 338}
				, {x: 600, y: 888}]
; choose a random number from 1 - arrayOfCoords Max
random, index, 1, % arrayOfCoords.count()
; pick the element out of the array
randomCoord := arrayOfCoords[index].clone()
click, % randomCoord.x " " randomCoord.y
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: random pick 3 squares in grid

08 Jun 2022, 00:49

Hallo,
shorter without clone():

Code: Select all

; choose a random number from 1 - arrayOfCoords Max
random, index, 1, % arrayOfCoords.count()
click, % arrayOfCoords[index].x " " arrayOfCoords[index].y
User avatar
Chunjee
Posts: 1501
Joined: 18 Apr 2014, 19:05
Contact:

Re: random pick 3 squares in grid

08 Jun 2022, 01:35

Once you've mastered that; use https://biga-ahk.github.io/biga.ahk/#/?id=samplesize to select three random elements from the array(no duplicates); then click each one.

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk


selection := A.sampleSize(arrayOfCoords, 3)
for key, value in selection {
	click, % value.x " " value.y
	sleep, 1000
}
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: random pick 3 squares in grid

08 Jun 2022, 07:08

Chunjee wrote:
07 Jun 2022, 23:22
I apologize this is probably the most advanced way to handle it; but you will learn objects on the way :) and I also think it is the most scalable to click more locations.

If we save one set of coordinates and click on them like so:

Code: Select all

setOfCoords := {x: 100, y: 333}
click, % setOfCoords.x " " setOfCoords.y
Then we can make an array of coordinates like so and choose randomly from it:

Code: Select all

arrayOfCoords := [{x: 100, y: 333}
				, {x: 200, y: 333}
				, {x: 300, y: 999}
				, {x: 400, y: 333}
				, {x: 500, y: 338}
				, {x: 600, y: 888}]
; choose a random number from 1 - arrayOfCoords Max
random, index, 1, % arrayOfCoords.count()
; pick the element out of the array
randomCoord := arrayOfCoords[index].clone()
click, % randomCoord.x " " randomCoord.y
Thank you - this is the way I want to go - precise points to click . I will try it out and post back here :)
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: random pick 3 squares in grid

08 Jun 2022, 13:30

I got that work fine thanks..
How do I pic 3 sets of coordinates without picking the same one twice ?
User avatar
Chunjee
Posts: 1501
Joined: 18 Apr 2014, 19:05
Contact:

Re: random pick 3 squares in grid  Topic is solved

08 Jun 2022, 17:51

Chunjee wrote:
08 Jun 2022, 01:35
use https://biga-ahk.github.io/biga.ahk/#/?id=samplesize to select three random elements from the array(no duplicates); then click each one.
You can also shuffle the array of coordinates and the first or last three will be both random, and non-repeating.


Vanilla ahk is a little more difficult; but here is perhaps a way with a little fn_noRepeatRand function that returns no-repeat numbers

Code: Select all

arrayOfCoords := [{x: 100, y: 333}
				, {x: 200, y: 333}
				, {x: 300, y: 999}
				, {x: 400, y: 333}]

; choose 3 random indexes (no repeats)
indexes := fn_noRepeatRand(1, arrayOfCoords.count(), 3)
for key, value in indexes {
	click, % arrayOfCoords[value].x " " arrayOfCoords[value].y
}

; functions
fn_noRepeatRand(start, stop, ammount)
{
	output := []
	memory := ""
	while(ammount != output.count()) {
		random, randomVar, % start, % stop
		if (inStr(memory, randomVar)) {
			continue
		}
		output.push(randomVar)
		memory .= randomVar ","
	}
	return output
}
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: random pick 3 squares in grid

08 Jun 2022, 19:26

Chunjee wrote:
08 Jun 2022, 17:51
Chunjee wrote:
08 Jun 2022, 01:35
use https://biga-ahk.github.io/biga.ahk/#/?id=samplesize to select three random elements from the array(no duplicates); then click each one.
You can also shuffle the array of coordinates and the first or last three will be both random, and non-repeating.


Vanilla ahk is a little more difficult; but here is perhaps a way with a little fn_noRepeatRand function that returns no-repeat numbers

Code: Select all

arrayOfCoords := [{x: 100, y: 333}
				, {x: 200, y: 333}
				, {x: 300, y: 999}
				, {x: 400, y: 333}]

; choose 3 random indexes (no repeats)
indexes := fn_noRepeatRand(1, arrayOfCoords.count(), 3)
for key, value in indexes {
	click, % arrayOfCoords[value].x " " arrayOfCoords[value].y
}

; functions
fn_noRepeatRand(start, stop, ammount)
{
	output := []
	memory := ""
	while(ammount != output.count()) {
		random, randomVar, % start, % stop
		if (inStr(memory, randomVar)) {
			continue
		}
		output.push(randomVar)
		memory .= randomVar ","
	}
	return output
}
@Chunjee That's is simply outstanding :) I so much appreciate the help people like yourself give in this forum - thank you
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: random pick 3 squares in grid

08 Jun 2022, 22:21

Or:

Code: Select all

arr  := [[100,300],[200,600],[300,1000],[400,400]], nArr := []
loop, 3 							; number of clicks;  max = arr.maxindex()
	{
	random, var, 1, arr.maxindex()
	nArr.push(arr.removeat(var))
	}
for a,b in nArr
	Click, % b.1 " " b.2
14.3 & 1.3.7
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: random pick 3 squares in grid

09 Jun 2022, 00:08

Or:

Code: Select all

Arr  := [[100,300],[200,600],[300,1000],[400,400]]
Loop, 3 ; number of clicks;  max = Arr.MaxIndex()
{
	Random, Var, 1, Arr.MaxIndex()
	Click,% (Var := Arr.RemoveAt(Var)).1 " " Var.2
}
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: random pick 3 squares in grid

09 Jun 2022, 00:20

:thumbup: (though it might be confusing for some to call the new array "var" as well)
14.3 & 1.3.7

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Hansielein, Lpanatt and 320 guests