Utilize a Gaussian rather than uniform random number for delay Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
claddyonfire
Posts: 3
Joined: 22 Apr 2021, 07:10

Utilize a Gaussian rather than uniform random number for delay

22 Apr 2021, 08:21

Hello!

I was hoping to find out a way to generate a random number (for use in a delay/sleep command) from a Gaussian distribution rather than a flat one. For example, if I'm trying to click consistently every ~1 minute while away from my computer, I would normally use a general click, sleep (rand, 50000, 70000), click loop. This would make the delay anywhere between 50-70 seconds between clicks, but there would be no weight towards any section of that range. Ideally, I would have all my clicks over a large period of time represent a normal/Gaussian distribution centered around 60,000 ms with a standard deviation of 5,000 ms, for example.

Is there a concise way of writing a script that would accomplish this? I was thinking something like a loop in which an integer is defined and assigned a value based on a given mean and standard deviation, but my thought was to use the mathematical function of a Gaussian curve with is... a bit cumbersome (roots, natural logs, etc.). I feel like there must be a much simpler/more concise way of doing this! Is there a Gaussian random command or something, which determines a random value given mean and std? Would the code be something like:

Code: Select all

loop 100
{
Click
int j = gaussian_random(mean, std)
sleep j
}
I just don't know what that integer assignment would need to be! Anybody have some advice? I really appreciate it!
User avatar
boiler
Posts: 16954
Joined: 21 Dec 2014, 02:44

Re: Utilize a Gaussian rather than uniform random number for delay

22 Apr 2021, 08:34

See this function. “Best” from that thread:

Code: Select all

rand_gaussian(standard_deviation, mean=0)
{
	max_random = 10000000
	Random, r1, 1, max_random ; 1 to prevent inf error
	Random, r2, 1, max_random
	Return mean + standard_deviation * Sqrt(-2 * Ln(r1 / max_random)) * Cos(2 * 3.14159265 * (r2 / max_random))
}
claddyonfire
Posts: 3
Joined: 22 Apr 2021, 07:10

Re: Utilize a Gaussian rather than uniform random number for delay

22 Apr 2021, 09:20

@boiler

Thank you! Sorry if this is a silly question, but how would I go about incorporating this command into a functional script? Do I define "rand_gaussian" at the beginning of the script and simply call it during the loop as

Code: Select all

rand_gaussian(5000, mean=60000)
{
	max_random = 10000000
	Random, r1, 1, max_random ; 1 to prevent inf error
	Random, r2, 1, max_random
	Return mean + 5000 * Sqrt(-2 * Ln(r1 / max_random)) * Cos(2 * 3.14159265 * (r2 / max_random))
}
loop 100
Click
Sleep rand_gaussian
or does it need to be established in this manner inside the loop for each execution? Also, I hope I'm correct in updating the code with actual values rather than "standard_deviation", or did I do that wrong? Thanks again for your help!
User avatar
boiler
Posts: 16954
Joined: 21 Dec 2014, 02:44

Re: Utilize a Gaussian rather than uniform random number for delay  Topic is solved

22 Apr 2021, 10:13

Yes, you define the function away from where you call it, but typically you would put functions at the bottom of your script. And you call it by adding the parameters you want to use in the function call, not by changing them in the function definition itself. That's the purpose of having parameters. You can pass different ones each time you call it. Also, you need { } around the commands that are meant to be part of the loop if you have more than one command inside the loop (see block). Here is how it would look:

Code: Select all

loop, 100
{
	Click
	Sleep, rand_gaussian(5000, 60000)
}
return

rand_gaussian(standard_deviation, mean=0)
{
	max_random = 10000000
	Random, r1, 1, max_random ; 1 to prevent inf error
	Random, r2, 1, max_random
	Return mean + standard_deviation * Sqrt(-2 * Ln(r1 / max_random)) * Cos(2 * 3.14159265 * (r2 / max_random))
}

The mean=0 in the function definition means that's the default value. So if you wanted to call the function and use the default value, you would just omit that paramter.:

Code: Select all

number := rand_gaussian(20)
claddyonfire
Posts: 3
Joined: 22 Apr 2021, 07:10

Re: Utilize a Gaussian rather than uniform random number for delay

22 Apr 2021, 10:35

Ahh awesome, thank you so much! Just tested it out (with shorter times) and it does exactly what I was looking for!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 182 guests