Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

generate random number


  • Please log in to reply
10 replies to this topic
azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
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

  • Guests
  • Last active:
  • Joined: --

Generates a pseudo-random number.
Source: http://www.autohotke...ands/Random.htm



Grendahl
  • Members
  • 416 posts
  • Last active: Jul 07 2014 08:01 PM
  • Joined: 10 Aug 2009
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
}

Always have your scripts when you need them with Dropbox.
Sign up for free! http://db.tt/9Hrieqj

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
thanks for your reply

how do I generate random number that is between 400-500 AND 600-700?

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Random, n, 400, 600

if (n >= 500)

    n := n + 100


aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
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, again
Yes, I know this code is clumsy. It is only intended for illustrative purposes. 8)
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

sinkfaze
  • Moderators
  • 6367 posts
  • Last active:
  • Joined: 18 Mar 2008
You can make it pretty simple in AHK_L:

Loop
	Random, n, 400, 700
Until	(n <= 500) || (n >= 600)
MsgBox, %n%


Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
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.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
@Frankie

It is only intended for illustrative purposes.

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.

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. :D

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 dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
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?

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
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:
again:
loop
 {
   tooltip, %a_index%
   random, n, 400, 700
   if (n > 410 && n < 690)
    {
      continue
    }
   else
    {
      break
    }
 }
msgbox, %n%
goto, again
With 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. :?
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact