 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
dnlooker Guest
|
Posted: Wed May 28, 2008 10:28 pm Post subject: Help with "random" function |
|
|
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
|
Posted: Wed May 28, 2008 10:30 pm Post subject: |
|
|
| whoops, that last return should be |
|
| Back to top |
|
 |
Trikster
Joined: 15 Jul 2007 Posts: 1224 Location: Enterprise, Alabama
|
Posted: Wed May 28, 2008 10:36 pm Post subject: |
|
|
| 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  _________________ ScriptPad | ~dieom | dieom | izwian2k7 | Ian | God |
|
| Back to top |
|
 |
dnlooker Guest
|
Posted: Wed May 28, 2008 10:42 pm Post subject: |
|
|
| wow, thanks IAN |
|
| Back to top |
|
 |
pokercurious
Joined: 16 Dec 2007 Posts: 47
|
Posted: Wed May 28, 2008 10:55 pm Post subject: |
|
|
| 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 |
|
 |
dnlooker Guest
|
Posted: Wed May 28, 2008 11:02 pm Post subject: |
|
|
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
|
Posted: Wed May 28, 2008 11:11 pm Post subject: |
|
|
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 |
|
 |
pokercurious
Joined: 16 Dec 2007 Posts: 47
|
Posted: Wed May 28, 2008 11:26 pm Post subject: |
|
|
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|