hello
many programming languages have a handy command that will generate a random number, given some specific criteria (eg, withing a range, etc)
is there anything similar in ahk?
thanks
generate random number
Started by
azure
, Oct 28 2011 10:42 AM
10 replies to this topic
#1
-
Posted 28 October 2011 - 10:42 AM
Generates a pseudo-random number.
Source: http://www.autohotke...ands/Random.htm
#2
-
Posted 28 October 2011 - 10:44 AM
As an example...
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
Else Random,r,a,b
Return r
}
#3
-
Posted 28 October 2011 - 06:56 PM
Always have your scripts when you need them with Dropbox.
Sign up for free! http://db.tt/9Hrieqj
Sign up for free! http://db.tt/9Hrieqj
thanks for your reply
how do I generate random number that is between 400-500 AND 600-700?
how do I generate random number that is between 400-500 AND 600-700?
#4
-
Posted 14 November 2011 - 08:38 AM
Random, n, 400, 600
if (n >= 500)
n := n + 100
#5
-
Posted 14 November 2011 - 11:23 AM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
I think this is closer to what azure asked for:
#persistent
again:
random, n, 400, 700
if (n > 500 && n < 600)
{
goto, again
}
msgbox, %n%
goto, againYes, I know this code is clumsy. It is only intended for illustrative purposes. 8)
#6
-
Posted 14 November 2011 - 03:19 PM
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
You can make it pretty simple in AHK_L:
Loop Random, n, 400, 700 Until (n <= 500) || (n >= 600) MsgBox, %n%
#7
-
Posted 14 November 2011 - 04:02 PM
Why call random an infinite number of times when it only need be called once? I don't care much for optimization, but there's no need for sabotage.
#8
-
Posted 14 November 2011 - 07:01 PM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
@Frankie
Perhaps this version will be more to your liking:
Edit:
I just saw that sinkfaze posted the same thing using an AutoHotkey_L "Until loop".
Sorry sinkfaze, I am used to AHk Basic and had to research "Until" in order to read your code. :oops:
My script as posted is only to show proof of concept. To show that no matter how many times it runs it will only show numbers within the requested ranges.It is only intended for illustrative purposes.
Perhaps this version will be more to your liking:
loop
{
random, n, 400, 700
if (n > 500 && n < 600)
{
continue
}
else
{
break
}
}
msgbox, %n%In theory this version could be used with little or no modificcation. It is also a lot less clumsy. Edit:
I just saw that sinkfaze posted the same thing using an AutoHotkey_L "Until loop".
Sorry sinkfaze, I am used to AHk Basic and had to research "Until" in order to read your code. :oops:
#9
-
Posted 15 November 2011 - 01:10 AM
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
interesting approach, however would I experience delay if the script generates many random numbers within the forbiden margins, until it finds a correct one?
also, moving to more advanced stuff, how can I get random numbers that satisfy a specific function, like x/4+5= 200 till 300, or something?
also, moving to more advanced stuff, how can I get random numbers that satisfy a specific function, like x/4+5= 200 till 300, or something?
#10
-
Posted 15 November 2011 - 04:07 PM
To answer your first question: It would depend on how narrow the respective zones are. In its current form my script most often only has to run once to get an "acceptable" number, and even when it runs 2-5 times there is no discernible delay.
Try this version:
Unfortunately I can't help with the second question, mostly because I do not understand it. Math is REALLY not my strong point. :?
Try this version:
again:
loop
{
tooltip, %a_index%
random, n, 400, 700
if (n > 410 && n < 690)
{
continue
}
else
{
break
}
}
msgbox, %n%
goto, againWith this the tooltip shows exactly how many iterations it had to go through to find an acceptable number. And as you can see, I narrowed the acceptable margin by a lot, but even when it has to go through as many as 70 iterations there is no noticeable delay.Unfortunately I can't help with the second question, mostly because I do not understand it. Math is REALLY not my strong point. :?
#11
-
Posted 16 November 2011 - 06:04 AM
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
~Albert Campion
-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact



Sign In
Create Account
Last active: Mar 18 2015 09:06 AM
Back to top
