AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with "random" function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
dnlooker
Guest





PostPosted: Wed May 28, 2008 10:28 pm    Post subject: Help with "random" function Reply with quote

why does this return "2 and 4" or "7 and 20" when I want it to return "7 and r" or something like that?
Code:
msgbox, % rand("number") " and " rand("letter")

rand(chr)
{
 if (%chr% := "number")
 {
  Random, r, 1, 9
  return, %r%
 }
 if (%chr% := "letter")
 {
  Random, r, 1, 26
  r:= Chr(96+r)
  return %a%
 }
}
Back to top
dnlooker
Guest





PostPosted: Wed May 28, 2008 10:30 pm    Post subject: Reply with quote

whoops, that last return should be
Code:
return, %r%
Back to top
Trikster



Joined: 15 Jul 2007
Posts: 1224
Location: Enterprise, Alabama

PostPosted: Wed May 28, 2008 10:36 pm    Post subject: Reply with quote

Code:
Rand(Min = 1, Max = 26, Type = "chr") {
   Random, v, %Min%, %Max%
   Return Type = "chr" ? Chr(96 + v) : Type = "num" ? v : "INVALID_TYPE"
}

MsgBox % Rand() . "`n`n" . Rand(1, 9, "num") . "`n`n" . Rand(1, 9, "type")


Short and sweet Smile
_________________
ScriptPad | ~dieom | dieom | izwian2k7 | Ian | God
Back to top
View user's profile Send private message
dnlooker
Guest





PostPosted: Wed May 28, 2008 10:42 pm    Post subject: Reply with quote

wow, thanks IAN
Back to top
pokercurious



Joined: 16 Dec 2007
Posts: 47

PostPosted: Wed May 28, 2008 10:55 pm    Post subject: Reply with quote

Code:
msgbox, % rand("number") . " and " . rand("letter")

rand(chr)
{
 if (chr = "number")
 {
  Random, r, 1, 9
  return r
 }
 if (chr = "letter")
 {
  Random, r, 1, 26
  r := Chr(96+r)
  return r
 }
}

checkit("this")

checkit(var)
{
  if (%var% := "hello")
  {
    msgbox % var . ": " . %var%
  }
}


Using ":=" assigns %chr% ('number' in the first call, 'letter' in the second) the value of "number" in the first if statement, which evaluates to true, so it hits the return and never gets to the second if statement (you'll notice that the way you have it, the second value is always 1-9, never 10-26).
Back to top
View user's profile Send private message
dnlooker
Guest





PostPosted: Wed May 28, 2008 11:02 pm    Post subject: Reply with quote

ok, I got all of the figured out and here is my code:
Code:
msgbox, % rand("num") " and " rand("chr") " and " rand("vou")

rand(chr)
{
 if (%chr% =num)
 {
  Random, r, 1, 9
  return, %r%
 }
 if (%chr% =chr)
 {
  Random, r, 1, 26
  return % Chr(96 + r)
 }
 if (%chr% =vou)
 {
  Random, r, 1, 5
   r1=a
   r2=e
   r3=i
   r4=o
   r5=u
  return
 }
}

as you've noticed, I have tried to add in a voul function as well but have yet to get it to work. Can anyone help me with this challenge?
Back to top
Trikster



Joined: 15 Jul 2007
Posts: 1224
Location: Enterprise, Alabama

PostPosted: Wed May 28, 2008 11:11 pm    Post subject: Reply with quote

This works too:

Code:
msgbox, % rand() " and " rand("chr") " and " rand("vou")

rand(Type = "num") {
   v1 := "a",v2 := "e",v3 := "i",v4 := "o",v5 := "u",num := "1:9",chr := "1:26",vou := "1:5"
   StringSplit, r, %Type%, :
   Random, v, %r1%, %r2%
   Return Type = "num" ? v : Type = "chr" ? Chr(96 + v) : Type = "vou" ? v%v% : "INVALID_TYPE"
}


A lil' longer than I wanted, but it gets the job done.
_________________
ScriptPad | ~dieom | dieom | izwian2k7 | Ian | God
Back to top
View user's profile Send private message
pokercurious



Joined: 16 Dec 2007
Posts: 47

PostPosted: Wed May 28, 2008 11:26 pm    Post subject: Reply with quote

Code:
msgbox, % rand("number") . " and " . rand("letter") . " and " . rand("vowel")

rand(chr)
{
 if (chr = "number")
 {
  Random, r, 1, 9
  return r
 }
 if (chr = "letter")
 {
  Random, r, 1, 26
  r := Chr(96+r)
  return r
 }
 if (chr = "vowel")
 {
  Random, r, 1, 5
  v1 = a
  v2 = e
  v3 = i
  v4 = o
  v5 = u
  return v%r%
 }
}


http://www.autohotkey.com/docs/commands/IfExpression.htm
http://www.autohotkey.com/docs/Variables.htm#Expressions

When you use the phrase "if ()", whatever's inside the "()" is treated as an expression, meaning you don't have to surround variable names with %% to access their contents.

Also, Ian's answer is really cool. Deeply nested ternary operators are cool.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group