Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Random Sleep Times?


  • Please log in to reply
20 replies to this topic
Roka
  • Guests
  • Last active:
  • Joined: --
Hi there, I recently found this site and was simply amazed at the amount of functionality AutoHotkey presents. I haven't been able to find out, though, if it's capable of one specific task I had in mind.

I want to write a Loop that has a random sleep times. I understand it's possible to do this by creating a variable and specifying certain values, but is it possible to have the sleep time vary anywhere between two values?

i.e. could I have the sleep time randomly be anywhere between 20 and 50 milliseconds?

Thanks! :D

purloinedheart
  • Members
  • 538 posts
  • Last active: Sep 22 2014 09:35 PM
  • Joined: 04 Apr 2008
Take a look at random

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
For exact sleep times you might want to use Delay() (It's the second one)
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

Roka
  • Guests
  • Last active:
  • Joined: --

Take a look at random


That's exactly what I'm trying to do! Thanks! I tried this:

Random, rand, 100, 1000
$1::
	Loop
	{
	if not GetKeyState ("1", "P")
	break
	Send 1
	Sleep rand
	}
return

And after pressing 1, it continued to print on notepad after I had released the key. What did I do wrong?

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Sleep %rand%
The %`s tell it it's a variable when using commands.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

None
  • Members
  • 3199 posts
  • Last active: Nov 05 2015 09:55 PM
  • Joined: 28 Nov 2009
Also you want the Random within the loop so it does not keep using the same value (or in a function :) ).
$1:: 
While GetKeyState("1", "P") {  ;while 1 is held
 Send 1 
 RandSleep(100,1000) ;sleep a random length of time
 }
return 

RandSleep(x,y) {
Random, rand, %x%, %y%
Sleep %rand%
}


roakbud
  • Guests
  • Last active:
  • Joined: --

Sleep %rand%
The %`s tell it it's a variable when using commands.


Thanks! :D

Also you want the Random within the loop so it does not keep using the same value.



How would I change the script to do that?

rokabud
  • Guests
  • Last active:
  • Joined: --
Oh I see it in your post now ty!

rokabud
  • Guests
  • Last active:
  • Joined: --

Also you want the Random within the loop so it does not keep using the same value (or in a function :) ).

$1:: 
While GetKeyState("1", "P") {  ;while 1 is held
 Send 1 
 RandSleep(100,1000) ;sleep a random length of time
 }
return 

[b]RandSleep(x,y) {
Random, rand, %x%, %y%
Sleep %rand%
}

[/b]


What does the bolded part mean?

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
It means "Look at me!"

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


rokabud
  • Guests
  • Last active:
  • Joined: --

Also you want the Random within the loop so it does not keep using the same value (or in a function :) ).

$1:: 
While GetKeyState("1", "P") {  ;while 1 is held
 Send 1 
 RandSleep(100,1000) ;sleep a random length of time
 }
return 

RandSleep(x,y) {
Random, rand, %x%, %y%
Sleep %rand%
}



Hmm, I added descriptions to the different lines with what I thought their function was, is this right?

$1:: 
While GetKeyState("1", "P") {  ;while 1 is held
 Send 1   ;sends 1
 RandSleep(100,1000) ;calls to the variable RandSleep and assigns 100 to x and 1000 to y
 }
return 

[b]RandSleep(x,y) {           ;creates a variable with an X and Y 
Random, rand, %x%, %y%            ;Uses the Random function to generate x and y
Sleep %rand%          ;makes the %rand% value the sleep time


purloinedheart
  • Members
  • 538 posts
  • Last active: Sep 22 2014 09:35 PM
  • Joined: 04 Apr 2008
Functions

Don't worry, it confuses everyone at first.

;Call this function
RandSleep(100,1000)

;You specify the x and y values, in the above example the x was 100 and the y was 1000
RandSleep(x,y) {
;Generate a random number between x(100) and y(1000)
Random, rand, %x%, %y%
;Sleep for the random amount
Sleep %rand%
}


x79animal
  • Members
  • 1021 posts
  • Last active: May 14 2013 04:21 PM
  • Joined: 01 May 2010
ive been using this one i made for a couple of years now

RandomSleep(min,max)
{
Random, random, %min%, %max%
Sleep %random%
}


Roka
  • Guests
  • Last active:
  • Joined: --
Thanks guys. Two more questions:

Can I have multiple hotkeys using the same random sleep thing? Or do I have to rewrite it for each key? i.e Can I have "X" and "Z" sharing the same one like this:

$X::
While GetKEyState("X", "P")
{
Send X
RandomSleep(45,75)
}
return

$Z::
While GetKeyState("Z", "P")
{
Send Z
RandomSleep(45,75)
}
return

RandomSleep(min,max)
{
Random, random, %min%, %max%
Sleep %random%
}

And my second question, how do I do this with mouse clicks? I read http://www.autohotke...mands/Click.htm

and came up with this:

#LButton::
While GetKeyState ("LButton", "P")
{
Click
RandomSleep(45,75)
}
return

It doesn't work, but I don't get any errors when launching it.

Roka
  • Guests
  • Last active:
  • Joined: --
Hmm OK I found http://www.autohotke.../topic69474.html and got it to work:

~$LButton::
While GetKeyState("LButton", "P")
{
Click
RandomSleep(45,75)
}
return

Why is the tilde needed? I read the Hotkeys page and it said

When the hotkey fires, its key's native function will not be blocked (hidden from the system). In both of the below examples, the user's click of the mouse button will be sent to the active window:


What does this mean? Does it mean that without the tilde, the mouse is still effectively being clicked, it's just not being used the way Windows uses it? (i.e. double clicking to open a file?)

Does that mean you could omit the tilde and have the script still work in games?